Solved

Dynamically Assign Instance Name

Posted on 2006-06-27
5
223 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:alanteigne
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 16995185
Hi alanteigne;

You can not do that in Visual Basic .Net. But what you can do is the following.

        ' Class level variable, HashTable
        Private htCycle As New Hashtable

        Dim strMachineID As String = "116"
        Dim c As New Cycle
        '...
        ' Initialize the class Cycle as needed
        '...
        htCycle.Add(strMachineID & "Cycle", c)
        ' Add all you Cycle class to the hash table with the name you want.

        ' To get a Cycle class from the hash table
        Dim cc As Cycle
        ' cc will have a reference to class 116Cycle
        cc = CType(htCycle("116Cycle"), Cycle)


I hope that this is of some help.

Fernando
0
 

Author Comment

by:alanteigne
ID: 16996330
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
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 16996460
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
0
 

Author Comment

by:alanteigne
ID: 17000446
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
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17000835
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
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question