Multidimensional Arrays - Save File/Load File
Multidimensional Arrays - Save File/Load File
Multidimensional Arrays - Save File/Load File
Multidimensional Arrays - Save File/Load File
Multidimensional Arrays - Save File/Load File
Multidimensional Arrays - Save File/Load File Multidimensional Arrays - Save File/Load File Multidimensional Arrays - Save File/Load File Multidimensional Arrays - Save File/Load File Multidimensional Arrays - Save File/Load File Multidimensional Arrays - Save File/Load File Multidimensional Arrays - Save File/Load File Multidimensional Arrays - Save File/Load File
Multidimensional Arrays - Save File/Load File Multidimensional Arrays - Save File/Load File
Multidimensional Arrays - Save File/Load File
Go Back  Xtreme Visual Basic Talk > > > Multidimensional Arrays - Save File/Load File


Reply
 
Thread Tools Display Modes
  #1  
Old 05-13-2012, 01:31 PM
KrisDwyer's Avatar
KrisDwyer KrisDwyer is offline
Newcomer
 
Join Date: Jun 2008
Posts: 20
Default Multidimensional Arrays - Save File/Load File


Hi,

I'm currently developing an editor for an AFL management sim.

I want to be able to load three or four multidimensional arrays to the program, then save them to the same file.

I tried the tutorial on here but got completely muddled up. Why can't it be easy like in VB6 When it was like 10 lines of code tops!
Reply With Quote
  #2  
Old 05-13-2012, 02:51 PM
Gruff's Avatar
GruffMultidimensional Arrays - Save File/Load File Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

It might help if you described your multidimensional array type. Is it 2d?

depending on complexity VB.Net can read or write to a file with one line of code.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #3  
Old 05-13-2012, 11:07 PM
KrisDwyer's Avatar
KrisDwyer KrisDwyer is offline
Newcomer
 
Join Date: Jun 2008
Posts: 20
Default

It's 3d, that is League(100, 50, 50) being the array.
Reply With Quote
  #4  
Old 05-14-2012, 12:33 AM
passel's Avatar
passelMultidimensional Arrays - Save File/Load File passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

A 3d array of what type?
If it is a standard type you can just use a BinaryFormatter to write and read the array.
If it is an array of structures or a class that you've defined, then you can still use the BinaryFormatter, but you have to tag the structure or class as being serializable.
Example code showing saving and reading a 3d array of a user defined structure.
Also, tested and commented out a version that is simply a 3d array of Doubles.
Code:
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary

Public Class Form1
  <Serializable()> Private Structure GameMap
    Public DestX As Integer
    Public DestY As Integer
    Public SourceX As Integer
    Public SourceY As Integer
    Public PrevTileX As Integer
    Public PrevTileY As Integer
    Public Walkable As Boolean
    Public Transparent As Boolean
    Public Animated As Boolean
    Public Animation_Direction As String
  End Structure

  Dim testArry3(,,) As GameMap

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim fs As New FileStream("DataFile.dat", FileMode.Create)

    ' Construct a BinaryFormatter and use it to serialize the data to the stream.
    Dim formatter As New BinaryFormatter
    Try
      ReDim testArry3(3, 4, 5)
      testArry3(0, 0, 0).DestX = 1
      testArry3(0, 0, 0).DestY = 1
      testArry3(0, 0, 0).Animation_Direction = "Down"

      testArry3(3, 4, 5).DestX = 4
      testArry3(3, 4, 5).DestY = 5
      testArry3(3, 4, 5).Animation_Direction = "Right"

      formatter.Serialize(fs, testArry3)
    Catch ex As SerializationException
      Console.WriteLine("Failed to serialize. Reason: " & ex.Message)
      Throw
    Finally
      fs.Close()
    End Try

  End Sub

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim fs As New FileStream("DataFile.dat", FileMode.Open)
    Try
      Dim formatter As New BinaryFormatter

      testArry3 = formatter.Deserialize(fs)

      Debug.Print(testArry3(3, 4, 5).Animation_Direction)
      Debug.Print(testArry3(0, 0, 0).Animation_Direction)

    Catch ex As SerializationException
      Console.WriteLine("Failed to deserialize. Reason: " & ex.Message)
      Throw
    Finally
      fs.Close()
    End Try

  End Sub


  'Dim testArray3(3, 4, 5) As Double

  'Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  '  Dim fs As New FileStream("DataFile.dat", FileMode.Create)

  '  ' Construct a BinaryFormatter and use it to serialize the data to the stream.
  '  Dim formatter As New BinaryFormatter
  '  Try
  '    For i As Integer = 0 To 3
  '      For j As Integer = 0 To 4
  '        For k As Integer = 0 To 5
  '          testArray3(i, j, k) = CDbl(i * 100 + j * 10 + k)
  '        Next
  '      Next
  '    Next

  '    formatter.Serialize(fs, testArray3)
  '  Catch ex As SerializationException
  '    Console.WriteLine("Failed to serialize. Reason: " & ex.Message)
  '    Throw
  '  Finally
  '    fs.Close()
  '  End Try
  'End Sub

  'Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  '  Dim fs As New FileStream("DataFile.dat", FileMode.Open)
  '  Try
  '    Dim formatter As New BinaryFormatter

  '    testArray3 = formatter.Deserialize(fs)
  '    Debug.Print(testArray3(3, 4, 5))
  '    Debug.Print(testArray3(1, 2, 3))

  '  Catch ex As SerializationException
  '    Console.WriteLine("Failed to deserialize. Reason: " & ex.Message)
  '    Throw
  '  Finally
  '    fs.Close()
  '  End Try

  'End Sub
End Class
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #5  
Old 05-14-2012, 01:30 AM
KrisDwyer's Avatar
KrisDwyer KrisDwyer is offline
Newcomer
 
Join Date: Jun 2008
Posts: 20
Default

It's just an array of Strings, declared as 'Public League(100, 50, 50) As String'

Want to be able to just save and recall it from a text file is all.
Reply With Quote
  #6  
Old 05-14-2012, 10:18 AM
AtmaWeapon's Avatar
AtmaWeaponMultidimensional Arrays - Save File/Load File AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I think a 3D array is a mistake for the data structure. I'd like to know what the structure of this array is; I bet there's a more logical way to design your model.

That's the biggest part of the problem. You're looking for a tutorial to write 3D arrays to files. No one who knows what they're doing uses them unless some really specific criteria are met. They're difficult to model in your head, so most people try to avoid using them at all costs. So there aren't many tutorials, since no one thinks it's helpful. Nonetheless, passel's example is a good tour of the "easiest" method. Serialization is more or less a one-liner but is difficult if you want files you can hand-edit. The only other alternative is to define a file format and write code to read and write it.

Here's the simplest possible thing, writing one item per line and assuming there are no elements with Nothing:
Code:
' _data is the name of the array.

Sub WriteData()
    Using writer As New StreamWriter(File.OpenWrite("yourfile"))
        For x As Integer = 0 To _data.GetUpperBound(0)
            For y As Integer = 0 To _data.GetUpperBound(1)
                For z As Integer = 0 To _data.GetUpperBound(2)
                    writer.WriteLine(_data(x, y, z))
                Next
            Next
        Next
    End Using
End Sub

Sub ReadData()
    Using reader As StreamReader = File.OpenText("yourfile")
        For x As Integer = 0 To _data.GetUpperBound(0)
            For y As Integer = 0 To _data.GetUpperBound(1)
                For z As Integer = 0 To _data.GetUpperBound(2)
                    _data(x, y, z) = reader.ReadLine()
                Next
            Next
        Next
    End Using
End Sub
It's only more than 10 lines because looping over a 3D array requires a minimum of 7 lines. Lines of code is a terrible metric for complexity on its own, anyway.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Multidimensional Arrays - Save File/Load File
Multidimensional Arrays - Save File/Load File
Multidimensional Arrays - Save File/Load File Multidimensional Arrays - Save File/Load File
Multidimensional Arrays - Save File/Load File
Multidimensional Arrays - Save File/Load File
Multidimensional Arrays - Save File/Load File Multidimensional Arrays - Save File/Load File Multidimensional Arrays - Save File/Load File Multidimensional Arrays - Save File/Load File Multidimensional Arrays - Save File/Load File Multidimensional Arrays - Save File/Load File Multidimensional Arrays - Save File/Load File
Multidimensional Arrays - Save File/Load File
Multidimensional Arrays - Save File/Load File
 
Multidimensional Arrays - Save File/Load File
Multidimensional Arrays - Save File/Load File
 
-->