|
-
May 25th, 2004, 09:22 AM
#1
Thread Starter
Junior Member
How can I refer to variable by a string?
How can I refer to variable by a string?
Instead of calling a subroutine by
changeValue(testClass.a)
changeValue(testClass.d)
changeValue(testClass.b)
I want to tell the subroutine the respective variable by a string:
"Change the variable <var> of testClass with <var> as String" (or just testClass.<var>)
I already tried the TypeConverter-Class but without much success (I finally ended up with an error "System.NotSupportedException, TypeConverter cannot convert from System.String")
Here a simplified code to illustrate my problem (the actual problem is more complex, so I reduced it to the core problem):
Assume I have class "testClass" with some variables:
Dim a As Integer = 1
Dim b As Integer = 8
Dim c As Integer = 5
Dim d As Integer = 4
Dim e As Integer = 6
I also have an array with all variables of this class I want to use:
Dim varList(2) as String
varList(0)="a"
varList(1)="d"
varList(2)="b"
Now I want to cycle through all varaibles in varList() and change their values:
For i = 0 to varList.GetUpperBound
Call changeValue(varList(i)) ***
Next i
Sub changeValue(ByRef var As Integer) ***
var+=10
End Sub
Finally, this should result in
a=11 b=18 c=5 d=14 e=6
*** Of course I know that I can't hand-over a String to an Integer, it's just to indicate the problem
-
May 25th, 2004, 10:12 AM
#2
Have you tried using a Collection?
Not really sure what you are trying to achieve.
I don't live here any more.
-
May 25th, 2004, 10:36 AM
#3
Thread Starter
Junior Member
You mean adding variables to the class like you would add controls to a form during runtime?
With controls it's no problem as I have the .name property. But how is it possible to refer to a variable, if I only have the name (as a String) of the variable without the .name property?
Maybe I clarify (and simplify?) the problem:
Assume a testClass with variables:
.a=1, .b=6, .c=5, ..., .z=19
When the user press a key, the value of the respective variable will be given:
Msgbox("The value is: " & testClass.<keyPress>)
with <keyPress> as a String
--> I have only a String with the name of the variable, but I want to use the variable with that name in the String. If You might also say that the content of the String is a pointer to the variable.
-
May 25th, 2004, 12:07 PM
#4
PowerPoster
Hi,
How about passing on the asc code
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
May 25th, 2004, 01:00 PM
#5
Thread Starter
Junior Member
Originally posted by taxes
Hi,
How about passing on the asc code
As I said, I reduced the problem to the core (maybe my example was not that good). The actual problem is more complex involving not only chars but all sorts of other variables (boolean, integer, double, string, array (if possible))
Next example:
classMyCarPool.car="BMW"
classMyCarPool.bike="BMX"
classMyCarPool.bus="Neoplan"
...
Dim vehicle as String
vehicle = InputBox("Please enter a vehicle type?")
<Input leads to vehicle="car">
Msgbox("The " & vehicle & " is a " & classMyCarPool.***)
<Output will be "The car is a BMW">
How do I replace the *** with the content of my String vehicle in order to obtain classMyCarPool.car?
Hopefully the problem became clear (now).
-
May 25th, 2004, 03:27 PM
#6
take a look into System.Reflection , here's a quick example i knocked together for you ( but i didn't use an inputbox as it's not really recommended to use them in .NET )
VB Code:
Public Class Form1
Inherits System.Windows.Forms.Form
'/// all your Form's stuff to create it would be here , but we skip that.
'///
'///
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cpool As New classMyCarPool
Dim vehicle As String = "bmw"
Dim v As Reflection.MemberInfo() = GetType(classMyCarPool).GetMembers
For Each m As Reflection.MemberInfo In v
If m.Name = "Cars" Then
Dim objVehicle As Object = GetType(classMyCarPool.Cars).GetField(vehicle).GetValue(cpool)
MessageBox.Show("The " & m.Name & " name is: " & Convert.ToString(objVehicle) & " : it's value is ... " & Convert.ToInt32(objVehicle))
End If
Next
End Sub
End Class
Public Class classMyCarPool
Public Enum Cars
bmw = 1
ferrari = 2
vauxhall = 3
End Enum
Public Enum Bikes
bmx = 1
racer = 2
mountain = 3
End Enum
Public Enum bus
singledecker = 1
doubledecker = 2
greyhound = 3
End Enum
End Class
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
May 25th, 2004, 06:59 PM
#7
PowerPoster
Hi,
Sorry, I did not make myself clear.
In it's simplest form, as I understand you, you have a range of variables holding various values. You give the user the option of selecting the name of one of those variables, presumably from a list or combo. When the selection is made you then want to access the contents of the selected variable.
This is what was known in DOS as "Macro Substitution" the function of which can be achieved by the use of a collection - which you have rejected.
Do you have to use the properties of a class?
Have you considered using a multi dimension array to store the values at present held in separate variables where the first dimension represents the type of vehicle and the second dimension holds the name of the vehicle. If you wanted to reduce the coding, you could use two separate single dimension arrays.
Either way, you can access the chosen reference quite easily, either directly using the value of the choice or, if the choice is a letter, the asc value (e.g. if the letter chosen is "A" and the relative data is in strarray(0) then you search for strarray(asc("A")-65)).
Come to think of it, if you move away from the use of class properties you will make your task easy.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
May 26th, 2004, 05:10 AM
#8
Thread Starter
Junior Member
Thanks for all the help and suggestions.
I finally got the answer by one line in the code of dynamic_sysop.
As I know that my string always has the name of an existing variable, I won't need to cycle through all members of my class with the System.Reflection. So I access my variable directly by
VB Code:
Dim objVehicle As Object = GetType(classMyCarPool).GetField(vehicle).GetValue(GetType(classMyCarPool).GetField(vehicle).Name)
In order to manipulate the value of your variable, you have to use an additional variable:
VB Code:
Dim helpVar As String ***
helpVar = CType(objVehicle, String)
' change helpVar
objVehicle = CType(helpVar, Object)
GetType(classMyCarPool).GetField(vehicle).SetValue(GetType(classMyCarPool).GetField(vehicle).Name, objVehicle)
Now there is the next problem:
How can I set the correct type of my helpVar?
Though the process of doing it seems quite clear, it doesn't work:
VB Code:
Dim helpType As Type
helpType = objVehicle.GetType
Dim helpVar As helpType
I also tried it with TypeCode, but none of my variations didn't work out.
Any suggestions?
P.S.: Collection didn't work as the items were ReadOnly. CollectionBase would probably do the job, but then I would have to write a lot of functions for addind/removing/... all sorts of variables that I want to use.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|