Results 1 to 8 of 8

Thread: How can I refer to variable by a string?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    Stuttgart, Germany
    Posts
    29

    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

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    Have you tried using a Collection?

    Not really sure what you are trying to achieve.
    I don't live here any more.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    Stuttgart, Germany
    Posts
    29
    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.

  4. #4
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    Stuttgart, Germany
    Posts
    29
    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).

  6. #6
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    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:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.     '/// all your Form's stuff to create it would be here , but we skip that.
    4.     '///
    5.     '///
    6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.         Dim cpool As New classMyCarPool
    8.         Dim vehicle As String = "bmw"
    9.  
    10.         Dim v As Reflection.MemberInfo() = GetType(classMyCarPool).GetMembers
    11.         For Each m As Reflection.MemberInfo In v
    12.             If m.Name = "Cars" Then
    13.                 Dim objVehicle As Object = GetType(classMyCarPool.Cars).GetField(vehicle).GetValue(cpool)
    14.                 MessageBox.Show("The " & m.Name & " name is: " & Convert.ToString(objVehicle) & " : it's value is ... " & Convert.ToInt32(objVehicle))
    15.             End If
    16.         Next
    17.     End Sub
    18. End Class
    19.  
    20. Public Class classMyCarPool
    21.     Public Enum Cars
    22.         bmw = 1
    23.         ferrari = 2
    24.         vauxhall = 3
    25.     End Enum
    26.  
    27.     Public Enum Bikes
    28.         bmx = 1
    29.         racer = 2
    30.         mountain = 3
    31.     End Enum
    32.  
    33.     Public Enum bus
    34.         singledecker = 1
    35.         doubledecker = 2
    36.         greyhound = 3
    37.     End Enum
    38. 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]

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    Stuttgart, Germany
    Posts
    29
    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:
    1. 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:
    1. Dim helpVar As String   ***
    2. helpVar = CType(objVehicle, String)
    3. ' change helpVar
    4. objVehicle = CType(helpVar, Object)
    5. 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:
    1. Dim helpType As Type
    2. helpType = objVehicle.GetType
    3. 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
  •  



Click Here to Expand Forum to Full Width