locked
Can I name a variable using the contents of another variable and if so how do I do it? RRS feed

  • Question

  • User-158480776 posted

    Environment: Windows 8; WebMatrix; Visual Basic

    1. Can I name a variable using the contents of another variable and if so how do I do it?

    To use an example;

                         i.      A database field contains the value "UK1"

                       ii.      I have recovered that value to a variable called "Publisher_Code"

                      iii.      Another field in the database contains the value 6

                    iv.      I have recovered that value to a variable called "Series_Count"

                      v.      I now want to create a VB array variable called and sized somewhat like this -

    Dim Series_Codes_[the contents of Publisher_Code](Series_Count) As Array

       Bottom line - I don't know the syntax for invoking the contents of Publisher_Code while I'm declaring the variable.

    I have seen a similar query to this elsewhere and the reply included some references to using objects and the following C# code snippet, but I couldn't work out how to adapt that to VB and to my specific example above,

    var obj = { someprop:"someval", otherprop:… },
        name = "someprop";
    obj[name]; // "someval"

    The same post also included this line -

    "You can't really, variable names (identifiers) are static (as long as you don't use eval) in a scope."

    Eval doesn't seem to be a VB statement, so though I would be willing to use something that 'evaluates' the dim statement I again do not know how to effect such 'evaluation'.

    With hopes for a positive answer and thanks in anticipation

    Best regards

    Philip, Perth, Western Australia

    Tuesday, January 1, 2013 3:24 AM

Answers

  • User-489377026 posted

    Hi,

    Dim Series_Codes_[the contents of Publisher_Code](Series_Count) As Array

    You can't do this, declare a variable, you need specify its name, but you can use Dictionary type like below:

    Dim vartable = New Dictionary(Of String, String)()
    Dim temp As String = "test3"
    vartable("test1") = "test1"
    vartable("test2") = "test2"
    vartable(temp) = "tttttttt"
    

    var obj = { someprop:"someval", otherprop:… }, 
        name = "someprop"; 
    obj[name]; // "someval"

    That's javascript way, so you may try Dictionary type.

    Best Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 2, 2013 1:49 AM

All replies

  • User-489377026 posted

    Hi,

    Dim Series_Codes_[the contents of Publisher_Code](Series_Count) As Array

    You can't do this, declare a variable, you need specify its name, but you can use Dictionary type like below:

    Dim vartable = New Dictionary(Of String, String)()
    Dim temp As String = "test3"
    vartable("test1") = "test1"
    vartable("test2") = "test2"
    vartable(temp) = "tttttttt"
    

    var obj = { someprop:"someval", otherprop:… }, 
        name = "someprop"; 
    obj[name]; // "someval"

    That's javascript way, so you may try Dictionary type.

    Best Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 2, 2013 1:49 AM
  • User-158480776 posted

    Thank you  Smile

    Wednesday, January 2, 2013 9:37 AM