I have successfully load a dynamic array reading the numbers in a text file into form. But how can I save that array again into text file after I change some numbers.

9 Years
Discussion Span
Last Post by CodeDoctor

If your dynamic array is integer then,

Dim a() As Integer = {11, 22, 33, 44}
 System.IO.File.WriteAllLines("file.txt", Array.ConvertAll(a, New Converter(Of Integer, String)(Function(t As Integer) t.ToString())))

For string array,

Dim S() as String={"10","20"}
System.IO.File..WriteAllLines("file.txt", S)
Votes + Comments

How do you want to store the array? CSV? XML? Binary?

Another great .NET technique is to understand and implement Object Serialization. Here is an example:
Import the following namespace:

Imports System.Runtime.Serialization.Formatters.Binary

Create a new Structure:

<Serializable()> Public Structure Person
       Dim Name As String
       Dim Age As Integer
       Dim Income As Decimal
   End Structure

Now, create an instance of this structure and populate it.
Write it to your file:

P = New Person()
   P.Name = "Joe Doe"
   P.Age = 35
   P.Income = 28500
   BinFormatter.Serialize("c:\MyFile.txt", P)
FS = New System.IO.FileStream _
      ("c:\MyFile.txt", IO.FileMode.OpenOrCreate)
   Dim obj As Object
   Dim P As Person()
       obj = BinFormatter.Deserialize(FS)
       If obj.GetType Is GetType(Person) Then
           P = CType(obj, Person)
           ' Process the P objext
       End If
   Loop While FS.Position < FS.Length - 1

The object P object will now contain the Person object you serialized previously.

Hope this helps.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.