Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello there,
I have a long code. I give an example that looks like code below. The following example contains the same logic as my code.

How can I call a variable with a string?

So how can I call StrArr2 array from StrArr1 as the variable name in the first step of the loop?

VB
Dim StrArr1 As String() = {"StrArr2"}
Dim StrArr2 As String() = {"StrArr3", "StrArr4"}
Dim StrArr3 As String() = {"-1"}
Dim StrArr4 As String() = {"StrArr5"}
Dim StrArr5 As String() = {"-1"}

Dim StrArr = StrArr1
Dim StrList As New List(Of String)

Start:
For Each s As String In StrArr
    StrList.Add(s)
    If s <> "-1" Then StrArr = s : GoTo Start 
Next


What I have tried:

How can assign to value to StrArr
Posted
Updated 19-Feb-17 4:17am
v2
Comments
[no name] 19-Feb-17 9:17am    
"call a variable with a string?", you don't. Nothing you wrote here makes any sense at all to me. You don't call variables. You don't call arrays from other arrays. If this is the same "logic" as your real code, you should start over.
Peter Leow 19-Feb-17 9:22am    
I think you should tell us what you intend to achieve instead?
gacar 19-Feb-17 9:33am    
I would like to invoke many xml files that are bound to one with one. With the above example, I want to construct this structure.
Graeme_Grant 19-Feb-17 9:44am    
Show examples of the XML files that you want to work with and explain what you are trying to do with them.
Maciej Los 19-Feb-17 9:40am    
Not sure what you want to achieve. Can you explain it?

Not sure i understand you correctly, but there's no way to create variable programmatically from string. You can create a Dictionary(Of String, Object)[^] which can hold variable names and their addresses.

VB
Sub Main
	Dim StrArr1 As String() = {"StrArr2"}
    Dim StrArr2 As String() = {"StrArr3", "StrArr4"}
    Dim StrArr3 As String() = {"-1"}
    Dim StrArr4 As String() = {"StrArr5"}
    Dim StrArr5 As String() = {"-1"}

    Dim StrArr As Object

	Dim ListOfVariables As New Dictionary(Of String, Object)
	
	ListOfVariables.Add("StrArr1", StrArr1)
	ListOfVariables.Add("StrArr2", StrArr2)
	ListOfVariables.Add("StrArr3", StrArr2)
	ListOfVariables.Add("StrArr4", StrArr4)
	ListOfVariables.Add("StrArr5", StrArr5)
	
	StrArr = ListOfVariables("StrArr2")
	For Each s In StrArr
		Console.WriteLine(s)
	Next

End Sub


Returns:
StrArr3
StrArr4
 
Share this answer
 
 
Share this answer
 
I found this solution on here
Like as Maciej Los

 Dim StrArr1 As String() = {"StrArr2"}
        Dim StrArr2 As String() = {"StrArr3", "StrArr4"}
        Dim StrArr3 As String() = {"-1"}
        Dim StrArr4 As String() = {"StrArr5"}
        Dim StrArr5 As String() = {"-1"}

        Dim StrArr = StrArr1
        Dim StrList As New List(Of String)

        
Start:
        For Each s As String In StrArr
            StrList.Add(s)
            Dim variables As New Dictionary(Of String, String())
            variables(s) = {"StrArr2"} ' Set the value of the "variable"
            If s <> "-1" Then StrArr = variables(s) : GoTo Start
    Next
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900