Visual Basic >
Visual Basic Forums
>
Visual Basic Language
>
How do I save (and load) a 2 dimensional array
How do I save (and load) a 2 dimensional array
- Hi I'm new to VB and struggling with saving and loading to a file. I am using Visual Studio Express 2008.
I have an array as follows:
Dim m(100,100) As node
Public Structure node
Public i As Integer
Public s As String
Public b As Boolean
End Structure
and I'd like to save to a file the m array.
I am not particularly bothered about data security and looked at xml serialization but cannot get this to work to save the m array.
Is this even possible?
Please can someone provide the code for save & load? I'd be grateful as I've spent ages trying to learn and search for the answer.
Answers
- XML serialization is probabaly the best way to do it, but you may need to create separate lists - that is, process the XML stream as 100 one-dimensional lists. If you can indicate the structures and coding you were using for XML serialization, someone may be able to assist wth getting it to work.
- Marked As Answer byJeff ShanMSFT, ModeratorThursday, November 12, 2009 7:09 AM
- As someone who hasnt done anything with serialization this is what i would do:1) saving
Dim m(100, 100) As node ''-=create data set Dim ds As New DataSet ds.Tables.Add("Dim1") ds.Tables("Dim1").Columns.Add("i") ds.Tables("Dim1").Columns.Add("s") ds.Tables("Dim1").Columns.Add("b") ds.Tables("Dim1").Columns.Add("dim1") ds.Tables("Dim1").Columns.Add("dim2") ''-=put data into data set For dim1 As Integer = 0 To 100 For dim2 As Integer = 0 To 100 Dim row As DataRow = ds.Tables("Dim1").NewRow row.Item("i") = m(dim1, dim2).i.ToString If m(dim1, dim2).s Is Nothing Then row.Item("s") = "" Else row.Item("s") = m(dim1, dim2).s.ToString End If row.Item("b") = m(dim1, dim2).b.ToString row.Item("dim1") = dim1 row.Item("dim2") = dim2 ds.Tables("Dim1").Rows.Add(row) Next Next ''-=save dataset ds.WriteXml("data.xml")2) loadingIt's a lame way to do it but a way nonetheless :)''-=load data into data set ds = New DataSet ds.ReadXml("data.xml") ''-=put data into array Dim m2(100, 100) As node For Each row As DataRow In ds.Tables(0).Rows m2(row.Item("dim1"), row.Item("dim2")).i = row.Item("i") m2(row.Item("dim1"), row.Item("dim2")).s = row.Item("s") m2(row.Item("dim1"), row.Item("dim2")).b = row.Item("b") Next
Cheers!- Marked As Answer byJeff ShanMSFT, ModeratorThursday, November 12, 2009 7:09 AM
All Replies
- XML serialization is probabaly the best way to do it, but you may need to create separate lists - that is, process the XML stream as 100 one-dimensional lists. If you can indicate the structures and coding you were using for XML serialization, someone may be able to assist wth getting it to work.
- Marked As Answer byJeff ShanMSFT, ModeratorThursday, November 12, 2009 7:09 AM
- As someone who hasnt done anything with serialization this is what i would do:1) saving
Dim m(100, 100) As node ''-=create data set Dim ds As New DataSet ds.Tables.Add("Dim1") ds.Tables("Dim1").Columns.Add("i") ds.Tables("Dim1").Columns.Add("s") ds.Tables("Dim1").Columns.Add("b") ds.Tables("Dim1").Columns.Add("dim1") ds.Tables("Dim1").Columns.Add("dim2") ''-=put data into data set For dim1 As Integer = 0 To 100 For dim2 As Integer = 0 To 100 Dim row As DataRow = ds.Tables("Dim1").NewRow row.Item("i") = m(dim1, dim2).i.ToString If m(dim1, dim2).s Is Nothing Then row.Item("s") = "" Else row.Item("s") = m(dim1, dim2).s.ToString End If row.Item("b") = m(dim1, dim2).b.ToString row.Item("dim1") = dim1 row.Item("dim2") = dim2 ds.Tables("Dim1").Rows.Add(row) Next Next ''-=save dataset ds.WriteXml("data.xml")2) loadingIt's a lame way to do it but a way nonetheless :)''-=load data into data set ds = New DataSet ds.ReadXml("data.xml") ''-=put data into array Dim m2(100, 100) As node For Each row As DataRow In ds.Tables(0).Rows m2(row.Item("dim1"), row.Item("dim2")).i = row.Item("i") m2(row.Item("dim1"), row.Item("dim2")).s = row.Item("s") m2(row.Item("dim1"), row.Item("dim2")).b = row.Item("b") Next
Cheers!- Marked As Answer byJeff ShanMSFT, ModeratorThursday, November 12, 2009 7:09 AM
Many thanks to you naurispunk. Nice one.
If the VB lanugage creators can't implement something like support for saving multidimensional ararys e.g m.Writexml("data.xml") which would be elegant, you have to create something else. Lame or not, just tried your code and it work immediately.
Thank you.- A lower tech solution might be to join the values in a delimited string (commas between entries, semi-colons between rows, or something). Then you can use Split to read the values and break them apart.
But naurispunk's solution is quite elegant.
Rod
Rod Stephens
www.vb-helper.com


