Link to home
Start Free TrialLog in
Avatar of alanteigne
alanteigne

asked on

Dynamically Assign Instance Name

Hello, I'm working on my first .NET project, and have a need similar to what I've done before in Actionsctipt.  I'm creating multiple instances of a custom class "Cycles," and would like to name them based on their machineID.  In Actionscript, I would so something like:

Eval(strMachineID + "Cycle") = New Cycle(startTime, stopTime) ...or something similar

This would result with an instance of the Cycle class named 116Cycle if the machine's id was 116.  I could then have multiple instances of the Cycle class (one per machine) and be able to reference them.  That way, no matter how many machines were added or removed, my class instances would be made to match.  

Thus far I have been unable to find anything regarding dynamically naming and referencing class instances in VB .NET.  Am I missing something simple?  Or... is there a better way to do it that someone could explain?

Thanks,
Alan
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alanteigne
alanteigne

ASKER

Fernando-

Thanks for the tip, I will try that next.  Using this method, is there a way to destroy one of those instances without affecting the rest of them?

Thanks,
alan
Alan;

If you execute a Remove method call on the hash table like this, assuming you have a Cycle class called 116Cycle:

        Dim cycleObjectName As String = "116Cycle"
        ...
        htCycle.Remove( cycleObjectName )

and as long as no other variable holds a reference to the object it will be garbage collected. All other Cycle objects in the hash table stay unaffected by that one remove. To remove all objects from the hash table you can do this

        htCycle.Clear( )

Fernando
Fernando-

Thanks!  I'll go ahead and hit accept.  If it's a quick answer, could you also tell me how I can see if an instance exists... something like...

If htCycle.exists(cycleObjectName) Then
     Do whatever
End If
Hi Alan;

You can find out if a object exists with the following statement.

     If htCycle.ContainsKey(cycleObjectName) Then
          Do whatever
     End If

This is a link to the documentation for the HashTable Class with other methods and properties that it has.

     http://msdn2.microsoft.com/en-us/library/system.collections.hashtable_members.aspx

Fernando