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
HowandWhy
Member
13 Points
43 Posts
Can I name a variable using the contents of another variable and if so how do I do it?
Jan 01, 2013 07:24 AM|LINK
Environment: Windows 8; WebMatrix; Visual Basic
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 -
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
) 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
Mark - MSFT
Contributor
7071 Points
435 Posts
Microsoft
Re: Can I name a variable using the contents of another variable and if so how do I do it?
Jan 02, 2013 05:49 AM|LINK
Hi,
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"That's javascript way, so you may try Dictionary type.Best Regards
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
HowandWhy
Member
13 Points
43 Posts
Re: Can I name a variable using the contents of another variable and if so how do I do it?
Jan 02, 2013 01:37 PM|LINK
Thank you