Results 1 to 5 of 5

Thread: Serialization

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026

    Serialization

    I have the following class file...

    VB Code:
    1. <Serializable()> Public Class GroupMember
    2.     Public Name As String
    3.     Public ID As String
    4.     Public Random As Double
    5. End Class
    6.  
    7. <Serializable()> Public Class GroupMemberCollection
    8.     Inherits System.Collections.CollectionBase
    9.  
    10.     Public GroupName As String
    11.     Public GroupStatus As String
    12.  
    13.     Public Sub Add(ByVal Member As GroupMember)
    14.         ' Invokes Add method of the List object to add a widget.
    15.         List.Add(Member)
    16.     End Sub
    17.  
    18.     Public Sub Remove(ByVal index As Integer)
    19.         ' Check to see if there is a GroupMember at the supplied index.
    20.         If index > Count - 1 Or index < 0 Then
    21.             ' If no GroupMember exists, a messagebox is shown and the operation is
    22.             ' cancelled.
    23.             System.Windows.Forms.MessageBox.Show("Index not valid!")
    24.         Else
    25.             ' Invokes the RemoveAt method of the List object.
    26.             List.RemoveAt(index)
    27.         End If
    28.     End Sub
    29.  
    30.     Public ReadOnly Property Members(ByVal index As Integer) As GroupMember
    31.         Get
    32.             ' The appropriate item is retrieved from the List object and
    33.             ' explicitly cast to the GroupMember type, then returned to the
    34.             ' caller.
    35.             Return CType(List.Item(index), GroupMember)
    36.         End Get
    37.     End Property
    38. End Class

    I am using Serialization to save instances of this class (group files)... My problem is that when it serializes, it only saves the information for the last index of Members within the GroupMembersCollection...

    So when I deserialize and try the following loop...

    VB Code:
    1. For i = 0 to GroupCollection.Count - 1
    2.    MsgBox(GroupCollection.Members(i).Name)
    3. Next i

    I get the last name in the collection over and over and over again.

    Can someone help me here?

    Thanks,

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    It sounds like you are readding the same reference to a member. Post your code for adding members. You should be calling New on every member object otherwise you are just changing the same instance.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    I will have to do that tomorrow morning.

    I think you may be correct, but I also think I checked for that...

    If I am editing the same instance of the Member object, then why is it that it repeats the output (name or whatever) for the number of times equal to the number of members I should have added to the Group Collection? If I were editing the same instance, would it do this?

    I believe the problem is when I serialize the Group Collection. Do I need to serialize each item in the collection seperately or should this be done when I serialize the whole collection?

    Thanks in advance,

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    A collection doesn't need unique instances so it will let you keep adding in the same instance. So yes it would do that. And since you are using the index then it will show for each indexed entry in the collection.

    If you serialize the collection it will serialize all items as well. NOTE: If you are using XML serialization then you have to mark the items part with a special attribute XMLArray or something like that for it to know its a collection of items.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    Nope. I'm using binary encryption... Some of the data is sensitive, so doing so will make the users a little happier... I thought of using XML for web publishing purposes later, but it's too much of a hastle.

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

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