Results 1 to 7 of 7

Thread: Multiple Objects

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2008
    Location
    Palm Desert, CA
    Posts
    91

    Multiple Objects

    Could someone post some example code for the following problem? I have a single class that I would like to create multiple objects of. Since I would like to do this on the go, I can't name each object. How would you do this. How could there be an array of objects and so on. The class represents a character in a game and I need to generate several randomly. Any help is appreciated thanks.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Multiple Objects

    If you know the number of elements then use an array:
    vb.net Code:
    1. Dim characters(characterCount) As Character
    2.  
    3. characters(0) = New Character
    4. 'etc.
    If you don't know how many items there will be then use a collection:
    vb.net Code:
    1. Dim characters As New List(Of Character)
    2.  
    3. characters.Add(New Character)
    4. 'etc.
    Either way you can access individual Character objects by numeric index or else you can enumeratr the whole list using a For Each loop.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2008
    Location
    Palm Desert, CA
    Posts
    91

    Re: Multiple Objects

    How do I identify the object instance?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Multiple Objects

    How do you need to identify them? Like I said, in both cases you can access a specific object by its numeric index. If you need to be able to identify them by a key of some sort then you might us a Dictionary instead of a List, but do you really need to identify the objects by a key? I'm not saying that you don't but it's far from a given that you do.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Multiple Objects

    This is another fine example of why you should provide a full and clear description of what you want upfront. If we knew what these objects represented and how they were to be used in the app then we could likely provide more specific advice. If we're given the bare minimum of information though, then we can only give very general, vague recommendations or else make assumptions about your project that may be completely false.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2008
    Location
    Palm Desert, CA
    Posts
    91

    Re: Multiple Objects

    Sorry I'll be more specific. I have an mdi form with two different type of child forms. 1 form is a list view and the other form can be opened several times since its an input form. After filling out the form the user can click on add to add the items to the listview form and then close down the input form. In the listview form if the user double clicks on an entry it should open the input form all filled out where now the user can click remove. So far the data is just between controls. I want to be able to use a class object to keep the data persistent. I don't know where I should initialize and instantiate the class, I have 3 forms the mdiparent the inputform and the listform. They have to communicate through the class instantiation that you explained so far using generics.

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