Results 1 to 7 of 7

Thread: Variable Question: Use variable value to set another variable of that values name

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2007
    Posts
    63

    Question Variable Question: Use variable value to set another variable of that values name

    Okay here's my question.

    How would I use a variables value to be used to set or get another variables value?

    Here's an example.

    Let's say I have 3 variables but actually have many more

    House = "Big"
    Car = "Fast"
    Pet = "Dog"

    Now I have a function that queries a table to get some data and one of the fields is a variable name such as Pet

    something like

    VariableCheck = GetVariableFieldValue() // This returns Pet

    so VariableCheck = Pet

    So rather than having a Huge select case statement such as:

    Select Case VariableCheck
    Case "House"
    txtValue.text = House
    Case "Car"
    txtValue.Text = Car
    Case "Pet"
    txtValue.Text = Pet
    End Select

    I need some kind of varValueVariable function.

    txtValue.Text = VarValueOf(VariableCheck)

    and a set value of vunction

    SetVarValueOf(VariableCheck) = "Cat"


    Thanks for all your help
    Last edited by Mythos44; Jul 8th, 2010 at 02:05 PM.

  2. #2
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Variable Question: Use variable value to set another variable of that values name

    Quote Originally Posted by Mythos44 View Post
    Okay here's my question.

    How would I use a variables value to be used to set or get another variables value?

    Here's an example.

    Let's say I have 3 variables but actually have many more

    House = "Big"
    Car = "Fast"
    Pet = "Dog"

    Now I have a function that queries a table to get some data and one of the fields is a variable name such as Pet

    something like

    VariableCheck = GetVariableFieldValue() // This returns Pet

    so VariableCheck = Pet

    So rather than having a Huge select case statement such as:

    Select Case VariableCheck
    Case "House"
    txtValue.text = House
    Case "Car"
    txtValue.Text = Car
    Case "Pet"
    txtValue.Text = Pet
    End Select

    I need some kind of varValueVariable function.

    txtValue.Text = VarValueOf(VariableCheck)

    and a set value of vunction

    SetVarValueOf(VariableCheck) = "Cat"


    Thanks for all your help
    Um, if your GetVariableFieldValue() function returns "Pet", and you want to set a textbox value equal to that string, you don't need a switch statement, just do

    Code:
    txtValue.Text = VariableCheck
    Likewise for setting it's value, you would do:

    Code:
    VariableCheck = "Cat"
    Unless I just don't get it...

    Justin
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2007
    Posts
    63

    Re: Variable Question: Use variable value to set another variable of that values name

    Okay the problem is I do not havea function that will do this. I do not know how to do this.

    I want to reverance a variable by the string value of another variable which it's string value is the variable name of the first variable.

    FirstVar = "Pet"
    SecondVar = "FirstVar"

    I want to somehow use the value of SecondVar which is "FirstVar" to set FristVar variable's value or what ever variable name is in SecondVar.

    I'm not sure how to explain it further.

  4. #4
    Hyperactive Member Philly0494's Avatar
    Join Date
    Apr 2008
    Posts
    485

    Re: Variable Question: Use variable value to set another variable of that values name

    Quote Originally Posted by MonkOFox View Post
    Um, if your GetVariableFieldValue() function returns "Pet", and you want to set a textbox value equal to that string, you don't need a switch statement, just do

    Code:
    txtValue.Text = VariableCheck
    Likewise for setting it's value, you would do:

    Code:
    VariableCheck = "Cat"
    Unless I just don't get it...

    Justin
    Look at his first post again, if it returns Pet he would actually want the text to read "Dog" since that is what corresponds with the variable name Pet.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Variable Question: Use variable value to set another variable of that values name

    It seems like you might do this with a dictionary (of String,String), where the key is what you are calling the variable name and the value is just that. Therefore, there would be a dictionary with entries:

    "House","Big"
    "Car","Fast"
    "Girlfriend","Dog"

    or whatever your example actually was. Now, you can pass in the key, which is what your GetVariableFieldValue method is returning, and get out the value for that key, which is what you are looking for.
    My usual boring signature: Nothing

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Variable Question: Use variable value to set another variable of that values name

    You cant do what you described I'm afraid - I would suggest the same thing that Shaggy mentioned, using a Dictionary.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7

    Thread Starter
    Member
    Join Date
    Jan 2007
    Posts
    63

    Re: Variable Question: Use variable value to set another variable of that values name

    Thank you all for your help.

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