473,507 Members | 12,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple instances of same object ?

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!
Feb 28 '07 #1
6 6898
On Feb 27, 8:43 pm, Bugs <d...@spamonbugs.orgwrote:
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

Feb 28 '07 #2
Bugs wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Feb 28 '07 #3
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

"Bugs" <do**@spamonbugs.orgschreef in bericht
news:Fo******************************@comcast.com. ..
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!

Feb 28 '07 #4
rowe_newsgroups wrote:
<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.
Feb 28 '07 #5
rowe_newsgroups wrote:
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!
Feb 28 '07 #6
On Feb 28, 11:39 am, Bugs <d...@spamonbugs.orgwrote:
rowe_newsgroups wrote:
One common practice is to have the dictionary as a shared property inthe 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!
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

Feb 28 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
4894
by: Ohaya | last post by:
Hi, I'm trying to understand a situation where ASP seems to be "blocking" of "queuing" requests. This is on a Win2K Advanced Server, with IIS5. I've seen some posts (e.g.,...
1
5961
by: Vlad | last post by:
Is there any way to install multiple instances of the same windows service designed with VS.NET 2003? I tried copying the binaries into a separate folder and then copying registry entries for the...
11
5261
by: Mike | last post by:
Looking to find any information on how to properly configure multiple instances of DB2. This is on Win2k db2 ver 7.2. I am basically looking for information on how the multiple instance settings...
1
1913
by: Johan | last post by:
If a user happens to open multiple instances of her frontend, what kind of problems can occur? For example, one user reported that she was able to, once or twice, bypass the evaluation...
1
1262
by: W. Jordan | last post by:
Hello, I would like to instantiate multiple Cache instances within my web application, for instance, one for database related items, one for application configuration items, one for user...
4
3130
by: Mike | last post by:
Class A public objX I want to create 2 or more instances of Class A and have the same value for objX in all instances. Instance1 of Class A Instance2 of Class A Instance3 of Class A
0
1905
by: rbg | last post by:
Have a web application which uses Data Cache. I need to understand what happens when a new instance of the same web application is created for for serving concurrent clients. What happens when...
17
11837
by: Arjen | last post by:
Hi, I want to reload 2 divs at one click. Ive tried: <a href = "javascript:void(0);"...
3
9123
by: Marcin Kalicinski | last post by:
How do I use multiple Python interpreters within the same process? I know there's a function Py_NewInterpreter. However, how do I use functions like Py_RunString etc. with it? They don't take any...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7030
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7482
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5041
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.