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.

Recommended Answers

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)
Jump to Post

All 3 Replies

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

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)
commented: reputationCallBack(serkan) +9

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()
   Do
       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
   FS.Close()

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


Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.