Results 1 to 4 of 4

Thread: vb.net saving arrays

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    vb.net saving arrays

    hi

    im trying to save a class with initial sizes

    Code:
        Public Sub SaveMap(ByVal aMap As map, ByVal fileName As String)
    
            Dim ser As New XmlSerializer(GetType(map))
            Dim fs As New FileStream(fileName, FileMode.Create)
    
            ser.Serialize(fs, aMap)
    
            fs.Close()
            fs.Dispose()
    
        End Sub
    Code:
    Public Class map
    
        Public X As Integer = 10
        Public Y As Integer = 10
        'Public Tile(5, 10, 10) As TileRec
    
        Public test(5, 5) As Integer
    
        Public Structure TileRec
    
            Public X As Integer
            Public Y As Integer
            Public Tileset As Integer
    
        End Structure
    
    End Class
    actually it won't save the 'test(5,5) as integer'. if i change 'test(5,5) as integer' to test(5) it will save the map well. why wont it save the 'test(5,5)' ?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    Re: vb.net saving arrays

    Multidimensional array serialization is not suported with XML serializers. You can switch to using a binary serialization, but the tradeoffs are these:

    1) Binary makes for smaller files, but editing them by hand is nearly impossible.
    2) XML serialization makes much larger files that can be readily edited using anything that can read text files.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: vb.net saving arrays

    Quote Originally Posted by Shaggy Hiker View Post
    Multidimensional array serialization is not suported with XML serializers. You can switch to using a binary serialization, but the tradeoffs are these:

    1) Binary makes for smaller files, but editing them by hand is nearly impossible.
    2) XML serialization makes much larger files that can be readily edited using anything that can read text files.
    how will my code look then?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,480

    Re: vb.net saving arrays

    try this:

    vb.net Code:
    1. Public Sub SaveMap(ByVal aMap As map, ByVal fileName As String)
    2.  
    3.     Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
    4.     Dim fs As New FileStream(fileName, FileMode.Create)
    5.  
    6.     formatter.Serialize(fs, aMap)
    7.  
    8.     fs.Close()
    9.     fs.Dispose()
    10.  
    11. End Sub


    to deserialize:

    vb.net Code:
    1. Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
    2. Dim fs As New IO.FileStream(fileName, IO.FileMode.Open)
    3. aMap = DirectCast(formatter.Deserialize(fs), GetType(map))
    4. fs.Close()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width