2013-03-06 62 views
-1

在VB中,我想根據文本框中的整數值調用一個數組。變量名內的Visual Basic變量

陣列命名如下:ARR1,ARR2,ARR3等

所以,

Dim num1 as Integer = TextBox1.Text 

如果NUM1 = 6,然後在下面的代碼,我想它來調用arr6。

TextBox1.AppendText(arr(num1)) 

任何幫助或指導表示讚賞!

回答

1

.NET中沒有動態變量名稱(如PHP或其他腳本語言)。另外,您應該確保TextBox1.Text實際上是一個整數,否則您將拋出異常。

Dim num1 as Integer 
If Integer.TryParse(TextBox1.Text, num1) then 
'some logic here because it parsed, otherwise its not a number! 

end if 

你可以使用一個列表或其他泛型集合握住你的陣列,並通過它們在列表中的索引引用他們,如果你真的需要所有陣列「命名爲」不同。

Dim lst As New List(Of Integer()) 

Dim arr1 As Integer() = {1, 2, 3, 4, 5} 
Dim arr2 As Integer() = {2, 4, 6, 8, 9} 
lst.Add(arr1) 
lst.Add(arr2) 

dim arrToUse as Integer() = lst.Item(num1) 
+0

真是太遺憾了。我以爲我只是沒有使用正確的括號或大括號。我想我會尋找解決方法。感謝您的建議! – MisterNaux 2013-03-06 00:54:21

+0

我已經用可能的解決方法更新了我的答案 – pinkfloydx33 2013-03-06 00:54:44