Results 1 to 6 of 6

Thread: array of objects

  1. #1

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

    array of objects

    Why won't this work?

    VB Code:
    1. <Serializable()> Friend Class ExportObject
    2.     Friend Groups() As [b]New[/b] Group
    3.  
    4.     Friend Class Group
    5.         Friend GroupName As String
    6.         Friend Members() As Member
    7.  
    8.         Friend Sub AddMember(ByVal Name As String, ByVal ID As String)
    9.             'make the members object array big enough
    10.             ReDim Members(Members.GetUpperBound(0) + 1)
    11.  
    12.             Members(Members.GetUpperBound(0)).MemberName = Name
    13.             Members(Members.GetUpperBound(0)).MemberID = ID
    14.         End Sub
    15.     End Class
    16.  
    17.     Friend Class Member
    18.         Friend MemberName As String
    19.         Friend MemberID As String
    20.     End Class
    21.  
    22.     Friend Sub AddGroup(ByVal GroupName As String)
    23.         Try
    24.             'resize the group object array
    25.             ReDim Groups(Groups.GetUpperBound(0) + 1)
    26.             Groups(Groups.GetUpperBound(0)).GroupName = GroupName
    27.         Catch ex As System.NullReferenceException
    28.             'if this happens, the group object has not yet been initialized
    29.             ReDim Groups(0)
    30.             Groups(0).GroupName = GroupName
    31.         End Try
    32.     End Sub
    33. End Class

    It tells me that arrays can not be declared using "new"

    How am I supposed to make an object array from one of my classes?

    Thanks in advance,

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

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    You will have to loop through each element and instantiate it.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    And how do I do this if I do not know how many groups I will be creating?

    Is there a way to create the element and instantiate it at the same time?
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    I would suggest that you use a collection class of some sort when you have an unknown number of elements.

    Then you would just instantiate the element as you are adding it (or you can, anyway)

    VB Code:
    1. MyGroupCollection.Add(New Group())
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    So I have to define the group as being a collection?

    Could you demonstrate this for me... I'm not quite following what it is that you're trying to convey to me.

    ....

    use the code I posted if you want... nothing fancy, just to give me an idea of what I need to do

    ....


    Thanks again,

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

  6. #6

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

    I think I got it...

    Good 'ol MSDN did it again...

    Thanks for the tip,

    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