creating objects using the value in a variable

jarrod

New member
Joined
Apr 6, 2007
Messages
2
Programming Experience
1-3
hey guys,

im wondering if there is any way in VS2005 to create objects of a class, using a variable as the name for the object?

something like:

for var1 as integer = 0 to 10
dim object(var1) as classname
next

so i can create 10 objects of the 'classname' class?
e.g. object1, object2, object3, etc
any ideas?
 
yes.. they are called ARRAYS..

Dim myStrings(0 to 10) as String

myStrings(0) = "Hello"
myStrings(1) = "Goodbye"
 
yes, i know what arrays are, but when try and dim teh value of an array as an instanct of classAction,

Dim sport(5) As String
'declaring an array
sport(0) = "Soccer"
sport(1) = "Cricket"
sport(2) = "Rugby"
sport(3) = "Aussie Rules"
sport(4) = "BasketBall"
sport(5) = "Hockey"

Dim sport(0) As ActionClass

i get the error, "local variable 'sport'' is already declared in teh current block.
 
So, call it something else?

That's like giving birth to non-identical twins and calling them the same name.. They are clearly different, one has blonde hair, one has brown.. Why would you want to call them the same thing? If you shouted "John! COme here" how would you indicate you wanted blonde haired john and not brown haired john?
 
You've misunderstood jarrod's question. He actually wants to:

Dim Soccer as ActionClass
Dim Cricket as ActionClass
Dim Rugby as ActionClass....etc

That is, he wants to use the string content of sport() to create new class names.
 
>That is, he wants to use the string content of sport() to create new class names
You mean, instance names..

Damm.. now we got two people not using the correct terms when trying to explain something that only makes sense to them and not the rest of the world


If you want to associate an ActionClass with a String, you use a Dictionary:

Dim xyz as New Dictionary(Of String, ActionClass)

xyz.Add("Soccer", New ActionClass)
xyz.Add("Rugby", New ActionClass)
xyz.Add("Volleyball", New ActionClass)


xyz("Rugby").KickTheBall()

Or whatever..


And no. You cannot use the contents of a string variable as if it were source code, because source code is compiled before it is run.. Strings left behind are just that: strings. Not source code.
 
>That is, he wants to use the string content of sport() to create new class names
You mean, instance names..

Damm.. now we got two people not using the correct terms when trying to explain something that only makes sense to them and not the rest of the world

If you want to associate an ActionClass with a String, you use a Dictionary:

Dim xyz as New Dictionary(Of String, ActionClass)

xyz.Add("Soccer", New ActionClass)
xyz.Add("Rugby", New ActionClass)
xyz.Add("Volleyball", New ActionClass)


xyz("Rugby").KickTheBall()

Or whatever..


And no. You cannot use the contents of a string variable as if it were source code, because source code is compiled before it is run.. Strings left behind are just that: strings. Not source code.

Sigh. Yes, I meant instance. A typo on my part, apologies. The rest of your explanation is appreciated. The condescending attitude towards newbie posters is not. Thanks again anyway.
 
Last edited:
yes, i know what arrays are, but when try and dim teh value of an array as an instanct of classAction,

Dim sport(5) AsString
'declaring an array
sport(0) = "Soccer"
sport(1) = "Cricket"
sport(2) = "Rugby"
sport(3) = "Aussie Rules"
sport(4) = "BasketBall"
sport(5) = "Hockey"
Dim sport(0) As ActionClass

i get the error, "local variable 'sport'' is already declared in teh current block.

I'm not sure whether I understand your question properly. You want to store a class called 'ActionClass' in the 0th index of the sport array? The problem is that once you declare sport to be an array of strings, you can only put strings in it. One work around would be to instead do something like this:

VB.NET:
Dim sport(5) As [B]Object[/B]
'declaring an array
sport(0) = "Soccer"
sport(1) = "Cricket"
sport(2) = "Rugby"
sport(3) = "Aussie Rules"
sport(4) = "BasketBall"
sport(5) = "Hockey"

sport(0) = New ActionClass()

If TypeOf sport(0) Is ActionClass Then
    Debug.Print("sport(0) is of type ActionClass")
    Dim TheActionClass As ActionClass = CType(sport(0), ActionClass)
ElseIf TypeOf sport(0) Is String Then
    Debug.Print("sport(0) is of type String")
    Dim TheString As String = CType(sport(0), String)
End If

This is because String and ActionClass are both of type object so sport would accept either. To check what type of class an Object is, you can use TypeOf, and to convert it to that type you can use CType (or DirectCast). May I ask what you're trying to accomplish?
 
May I ask what you're trying to accomplish?

I think siphon was right in that the OP wants to name a variable in his code using the content of a string. As an example, a newbie programmer might want to put a text box on a form and ask the user of the program a question:

"I'm going to make an object of type ActionClass. What do you want to call the variable I will assign it to?" [TEXTBOX HERE]

The thing is.. so what if the user types "Soccer", and it is successful that "Soccer" is used as the variable name? What difference does it make? Variable names are for the programmer's use only, to make the code more meaningful. They are lost when the program is compiled, and they mean nothing to the compiler. The compiler wouldnt look at the name of an ActionClass typed variable being Soccer and treat it any differently to how it would if its name were Rugby, Cricket, Xksahdtasd or Qwe123ght41

Thats the crucial point here; variable names are meaningless to anyone other than the coder. The coder wouldnt bother putting all his variable names in a string, and then try to create objects with those strings as variable names.. He would jsut type the name into the code:

NO:
Dim s as String = "Soccer"
Dim s as ActionClass 'hope to make a variable called Soccer using contents of s

YES:
Dim s as String = "Soccer"
Dim soccer as ActionClass


If the OP is looking for extensability, that's different. These things are more useful in factory pattern setup and recall though..
 
Back
Top