Vb.net中的变量

Wil*_*ill 0 vb.net

这是我想要做的.我有一个循环并尝试连接它以生成字符串.无论出于何种原因,我得到test1,test2等而不是变量等于.我想要做的是从连接测试&cstr(a)获取值test1等....

dim test1, test2, test3, test4, test 5 as string
test1 = "The"
test2 = "dog"
test3 = "came"
test4 = "to"
test5 = "play"

for a = 1 to 5
  label1.text += test & cstr(a) & " " 
next
Run Code Online (Sandbox Code Playgroud)

小智 5

我不认为你可以动态创建变量名称,就像你试图用你test & cstr(a)的代码一样.但是,如果它是您的选择,请尝试这样的事情:

    Dim test1, test2, test3, test4, test5 As String
    test1 = "The"
    test2 = "dog"
    test3 = "came"
    test4 = "to"
    test5 = "play"

    Dim testArray As String() = New String() {test1, test2, test3, test4, test5}

    For a As Integer = 0 To testArray.Length - 1
        label1.Text += testArray(a) & " "
    Next
Run Code Online (Sandbox Code Playgroud)