开发者

Variables in Vb.net

Here is what im trying to do. I have a loop and trying to concatenate it to produce the string. For whatever reason im getting test1, test2, etc instead of the variable equals to. What i am trying to do is get the value test1, etc.... from the concatenating test & cstr(a)

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) & " " 
nex开发者_开发问答t


I don't think you can dynamically create the variable names like you're trying to do with your test & cstr(a) piece of code. However, try something like this instead if it's an option for you:

    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


To the best of my knowledge, there's no way in VB to generate a variable name dynamically and expect VB to be able to access it. You could accomplish something resembling what you want by creating an array and looping through the elements of the array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜