• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

World's easiest VB.net question...

AtlantaBob

Golden Member
Yes, this is silly. No, I can't figure out how to do it.

This is all VB.net (Console Application)

I've defined a class, and I want users to be able to create as many instances of it as they wish. Something akin to:

strInput = Console.Readline()

While strInput <> "d"
CREATE NEW INSTANCE OF CLASS WITH strInput as the name of the class
strInput = Console.Readline()
Loop

So, what does in the CREATE NEW INSTANCE OF CLASS Line?

Thanks...


 
Ummm...

Sorry, I don't understand. To create a new instance of a class, you do (VB.NET):

Dim variable as Class1
variable = New Class1

You can't dynamically name a variable at runtime if that's what you mean. Variable names are supposed to be for your reference.
 
xtnight,

That's my thinking too, but let's say I wanted the user to be able to create between 1 and 100 instances of a class.
I wouldn't create them all by stating

dim inst1 as New Class
dim inst2 as New Class
dim inst3 as New Class
...
dim inst100 as New Class, right?

seems awfully inefficient. Isn't there a better way to do that?

Thanks
 
I didn't understand your question. You mean you want the user to be able to enter how many objects and have you create that many? Simple. This is roughly what you do, but I haven't tested this.
 
Thanks, xtknight,

I actually don't want the user to have to input the values beforehand, but I think I can go from what you wrote there.

I guess I'll post back if I have any issues in the conversion....
 
This appears to work. Someone mind commenting if they see something silly or wrong with it?

Of course, also nice to comment to tell me that I did ok, too.

Thanks.
 
What .Net version are you using?

If .Net 2.0, then the Generics are much more efficient than redimming an array:

'generics make class collections easy to build
Dim coll As new List(Of Class)
coll.Add(new Class)
coll.Add(new Class)


For .Net 1.1, your code looks ok.
 
KB, Thanks for the headsup. I'm running .Net 2.0... I'll take a look at lists.

I realize this might be a little off topic, but, heck, it's my own thread. I'm eventually going to interate through and perform some calculations on all of these objects.

Because some of these characteristics are shared (e.g. think, the average value of all of these objects) I was thinking of having shared functions defined in the class to handle them. To do that, the most sensible thing to do (In my mind) was to add the objects to a shared, friend collection as soon as they were created in sub new(). That way, I thought that I could go through the members of the collection both within the class and in the main body of the program.

Does this make sense? and does this change the advice given above?

Once again, Thanks!
 
My program with collections is that you could never just address a specific object without looping through the whole collection, but maybe that doesn't apply to .NET.
 
Back
Top