Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I have two problems
1)
I have written a class and would like to create multiple instances of it without having a pre-defined limit(ie like a select case of dim statements instantiating and naming the new instance's)
I am new to programming but have read a couple of books ,but they only show you how to create a single instance,or a couple with pre defined names. I tried using a variable as the field name in the dim statement ,but it wont let me do that
Whats the technique for managing multiple instances

2)
I actually have two classes that I want multiple instances of say, "A" and "B" .Then I want say, the third instance of A to be able to call a method in the fifth instance of "B" ,with again the instances being made on the fly.
I have searched and read about references and pointers as a guess in Google but neither of them seem relevant,unless I just didnt get it

I will be more then happy with just something I can google and read up on

Thank you for your help
Posted

wrote:
I have written a class and would like to create multiple instances of it without having a pre-defined limit(ie like a select case of dim statements instantiating and naming the new instance's)


The way to create an arbitrary number of instances of an object, is to use a generic List, which you can then add to as often as you like.


wrote:
I actually have two classes that I want multiple instances of say, "A" and "B" .Then I want say, the third instance of A to be able to call a method in the fifth instance of "B" ,with again the instances being made on the fly.


You can use delegates to hook up methods between classes, but to do what you're describing in an arbitrary way sounds unworkable to me. You can certainly create a specific instance of both classes, and use a delegate to hook them to each other, but to do that in an arbitrary, ad hoc manner, I don't see a reason for it. Delegates are how you do it tho.
 
Share this answer
 
I will look into it .Delegates are not something I've had much to do with yet,thank you Christian
 
Share this answer
 
An alternative to Christian's suggestion would be to use a dictionary, which uses "keys" and "values". You can add your objects to the dictionary and retrieve them later using a key. The keys can be any type you want them to be.
 
Share this answer
 
There are good articles on delegates here on Code Project. They are basically a function definition that can point to a function in any class, so that calling the delegate, calls the function(s) it points to.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900