2011-09-19 73 views
0

這就是我試圖做的。我有一個循環,並試圖連接它來產生字符串。無論出於什麼原因,我得到了test1,test2等,而不是變量等於。 我所試圖做的就是價值爲test1,等....從串接測試& CSTR(一)Vb.net中的變量

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 
+0

這行中定義的'test'變量是什麼'label1.text + = test&cstr(a)&「」' –

+3

請寄出至少_compile_的代碼。這些關鍵字不是大寫字母,你的一個聲明變量有一個_space_。 – Oded

+0

Op正試圖建立變量名稱以獲取它們的值...如果你想要這樣的東西,你最好使用一個數組或任何其他結構。這個:label1.text + = test&cstr(a)&「」只會寫你發佈的內容,它不會得到變量內的值......而對於Oded,這是VB,所以不需要大寫任何東西。 – gbianchi

回答

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 
+0

但爲什麼不使用只是一個字符串數組?並從該數組構建文本? – gbianchi

0

據我所知,有一個在VB沒有辦法動態生成一個變量名,並期望VB才能夠訪問它。你可以通過創建一個數組並循環遍歷數組的元素來完成類似你想要的東西。