参考带变量的变量? [英] Referring to a variable with a variable?
问题描述
是否可以使用其他变量引用变量?
Is it possible to refer to a variable using another variable?
例如,
Dim var1 As String
Dim var1 As String
Dim var2 As String
Dim var2 As String
Dim var3 As String
Dim var3 As String
Dim vnumber As Integer
Dim vnumber As Integer
vnumber = 1
vnumber = 1
var(vumber)= 5
var(vumber) = 5
这似乎不起作用,因为我已经写过了,但有另一种方法可以实现相同的目标吗?
This doesn't seem to work as I've written it but is there another way to achieve the same objective?
如果没有,是否有一段代码可以在程序运行时生成一个全新的变量?
If not, is there a piece of code that can generate a whole new variable while the program is running?
谢谢,
A
推荐答案
这可以通过数组完成:
This can be done with arrays:
Dim vars(3) As String
Dim vnumber = 1
vars(vnumber) = "Text" ' write to vars(1)
元素 var(0)也是这个数组。
在添加新元素之前,可以使用 Redim Preserve 放大此数组。
This array can be enlarged using Redim Preserve before appending new elements.
还有其他类型的有用的集合: Dictionary , List 等。
There are other kinds of useful collections: Dictionary, List, etc.
这篇关于参考带变量的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!