Multiple instances of same object ?

  • Thread starter Thread starter Bugs
  • Start date Start date
B

Bugs

Does anyone have any recommendations on the best way to create multiple
instances of the same class when the final number of instances is unknown?

For example, I have a class and based on some user actions, I want to
create n number instances of those classes, which is obviously unknown
at compile time.

What is the easiest way to create and then manage these kinds of objects?
Using an array seems intuitive but it also seems like it might be more
difficult to manage if you want to add and delete objects.

Would it be best to maybe create a collection of the objects? If so,
how would I name those objects before adding them to the collection. Is
it possible to name an object instance based on a string literal? e.g.

Public Sub createMyNewClass(ByVal myStringLiteral as String)
Dim myStringLiteral As New myNewClass(myStringLiteral)

Except somehow the value of myStringLiteral is used as the object name,
not the name "myStringLiteral"?

Thanks!
 
Does anyone have any recommendations on the best way to create multiple
instances of the same class when the final number of instances is unknown?

For example, I have a class and based on some user actions, I want to
create n number instances of those classes, which is obviously unknown
at compile time.

What is the easiest way to create and then manage these kinds of objects?
Using an array seems intuitive but it also seems like it might be more
difficult to manage if you want to add and delete objects.

Would it be best to maybe create a collection of the objects? If so,
how would I name those objects before adding them to the collection. Is
it possible to name an object instance based on a string literal? e.g.

Public Sub createMyNewClass(ByVal myStringLiteral as String)
Dim myStringLiteral As New myNewClass(myStringLiteral)

Except somehow the value of myStringLiteral is used as the object name,
not the name "myStringLiteral"?

Thanks!

Lookup up Dictionary.

i.e.

<pseudocode>

Dim MyObjects as new Dictionary(Of String, Of MyClass)()

public sub CreateObjects(numberOfObjects as Int32)
for i as int32 = 0 to numberOfObjects
MyObjects.Add("Object" & i.ToString(), new MyClass())
next
end sub

</pseudocode>

Then you can reference any MyClass object by the key (the string value
here) and also can loop through either the keys or values if the need
arises.

One common practice is to have the dictionary as a shared property in
the class file, then in the class's constructor you add the class
instance to the dictionary, and in the dispose (or other suitable
method) you remove the instance from the dictionary.

Thanks,

Seth Rowe
 
Bugs said:
Does anyone have any recommendations on the best way to create multiple
instances of the same class when the final number of instances is unknown?

For example, I have a class and based on some user actions, I want to
create n number instances of those classes, which is obviously unknown
at compile time.

What is the easiest way to create and then manage these kinds of objects?
Using an array seems intuitive but it also seems like it might be more
difficult to manage if you want to add and delete objects.

Would it be best to maybe create a collection of the objects? If so,
how would I name those objects before adding them to the collection. Is
it possible to name an object instance based on a string literal? e.g.

Public Sub createMyNewClass(ByVal myStringLiteral as String)
Dim myStringLiteral As New myNewClass(myStringLiteral)

Except somehow the value of myStringLiteral is used as the object name,
not the name "myStringLiteral"?

Thanks!

You are mixing up variable names with references.

An object never has a name, but a reference variable that is used to
keep track of the object may have a name.

Example:

Dim sb As StringBuilder

This declares a reference variable that can reference a StringBuilder
object. The variable has a name, and it's type is a reference. It
doesn't contain any reference at this time, though.

sb = New StringBuilder()

Now we have created an object and assigned the reference to the
variable. The object doesn't have a name itself, but the reference
variable has the name "sb". We use the variable whenever we want to
access the object, so it's easy to think that the object has a name, but
it hasn't.


What you are trying to do when you want to use a string to name the
object, is to create variables dynamically. This is sometimes done in
scripting languages (mostly because the coder doesn't know how to do it
any other way), but it's not possible in a compiled language, as all
variable names are resolved at compile time.

There are different collections that you can use to keep references to
objects. You mentioned an array, which is fine if you know the number of
objects before you create the array. Otherwise there are many different
collections that you can use, like lists, dictionaries, queues and stacks.
 
Bugs,

My first thougt was you are just asking for the collection of a type which
is new for 2005 so not so new anymore List (of T)

The answer gave me however the idea that you just want to name your objects
as it is standard done and than you need no collection that is the normal
inbuild behaviur of an OOP program.

Class bycicles

Dim MyBike as bycicles
Dim HisBike as bycicles

Although this is not the same object. You cannot have multiple instances of
an object, only multiple instances of a class (type), while you can have
endless references to the same object.

Cor
 
rowe_newsgroups said:
<pseudocode>

Dim MyObjects as new Dictionary(Of String, Of MyClass)()

public sub CreateObjects(numberOfObjects as Int32)
for i as int32 = 0 to numberOfObjects
MyObjects.Add("Object" & i.ToString(), new MyClass())
next
end sub

</pseudocode>
Thanks Seth, that's exactly what I was looking for.
 
rowe_newsgroups said:
One common practice is to have the dictionary as a shared property in
the class file, then in the class's constructor you add the class
instance to the dictionary, and in the dispose (or other suitable
method) you remove the instance from the dictionary.
One followup question about collections in general:
Seems like most of the VB.NET books just talk about using a Collection
class, not the other collection classes such as Dictionary, Hashtable, etc.?

Is that because VB6's primary collection is the Collection class, a
backward compatibility issue? Are there any significant disadvantages
to using Collection instead of Dictionary, etc. in VB.NET 2005?
Thanks!
 
One followup question about collections in general:
Seems like most of the VB.NET books just talk about using a Collection
class, not the other collection classes such as Dictionary, Hashtable, etc.?

Is that because VB6's primary collection is the Collection class, a
backward compatibility issue? Are there any significant disadvantages
to using Collection instead of Dictionary, etc. in VB.NET 2005?
Thanks!

Are you sure you are reading about Vb 2005 (.Net framework 2.0)?
Generics (list, dictionary, et al) weren't available in previous
versions, so if you have a book about .Net 1.1 (vb 2003) it wouldn't
mention them since they didn't exist.


Thanks,

Seth Rowe
 
Back
Top