|
-
Aug 8th, 2008, 01:27 AM
#1
Thread Starter
Lively Member
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.
-
Aug 8th, 2008, 01:31 AM
#2
Re: Multiple Objects
If you know the number of elements then use an array:
vb.net Code:
Dim characters(characterCount) As Character characters(0) = New Character 'etc.
If you don't know how many items there will be then use a collection:
vb.net Code:
Dim characters As New List(Of Character) characters.Add(New Character) '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.
-
Aug 8th, 2008, 02:14 AM
#3
Thread Starter
Lively Member
Re: Multiple Objects
How do I identify the object instance?
-
Aug 8th, 2008, 02:31 AM
#4
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.
-
Aug 8th, 2008, 02:37 AM
#5
Re: Multiple Objects
You must be having some data in the object like fields, properties. You can use these to identify the objects.
-
Aug 8th, 2008, 04:17 AM
#6
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.
-
Aug 8th, 2008, 01:22 PM
#7
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|