2013-03-06 61 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