如何将数组中的值作为变量引用? [英] How do I reference the value in an array as a variable?

查看:285
本文介绍了如何将数组中的值作为变量引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我有数组:

DeviceArray = Array("Sw","Cap","Gen","Reg","Tr","Br")

我想将所有内容初始化为一个值,例如0,这等效于为此编写一个for循环,以便将数组中的值视为变量名,以便我可以间接修改变量值:

And I want to initialize everything to a value such as 0, what would be the equivalent to writing a for loop for this such that the value within the array is treated as a variable name instead so that I can indirectly modify variable values:

For i = 0 to ubound(DeviceArray)
     DeviceArray(i) = 0
Next i

这样,在运行代码之后,变量: Sw,Cap,Gen,Reg,Tr和Br都应设置为0.

Such that after running the code, the variables: Sw, Cap, Gen, Reg, Tr, and Br should all be set to 0.

将DeviceArray中的每个元素都更改为0,并且不再存储最初放置的字符串.

Versus every element in DeviceArray being changed to 0 and no longer storing the strings initially placed.

因此DeviceArray应该保持不变,并仍然存储值("Sw","Cap","Gen","Reg","Tr","Br")

So DeviceArray should remain unchanged and still store the values ("Sw","Cap","Gen","Reg","Tr","Br")

希望可以清楚地说明我要做什么,我正在尝试这样做,所以我不必键入:

Hope that clearly explains what I am trying to do, I am attempting to do this so I do not have to type:

Sw = 0
Cap = 0
Gen = 0
Reg = 0
Tr = 0
Br = 0

因为在我的宏中有很长的变量需要不断地初始化为不同的值.

Because there is a long list of variables that need to be constantly reinitialized to different values throughout my macro.

例如DeviceArray(0) = Sw,但是我希望VBA宏将DeviceArray(0)识别为变量Sw的名称,这样我可以修改变量Sw的值而无需直接调用它. /p>

So for example DeviceArray(0) = Sw, but I want the VBA macro to recognize DeviceArray(0) as the name of a variable Sw instead such that I can modify the value of the variable Sw without calling it directly.

推荐答案

听起来您正在寻找引用类型而不是值类型.为此,您将需要VBA中的一个对象.

It sounds like you are looking for a reference type rather than a value type. For this, you will need an Object in VBA.

例如,创建一个名为Device的类模块.简单地说,您可以将Public value作为类中的唯一代码.

For example, create a Class Module called Device. At its simplest you could just put Public value as the only code within the Class.

'Class Device
Public value

现在,您可以按名称引用对象并传递这些引用,例如按照您的示例将它们放入Array.

Now you can reference the Objects by name and pass around those references, e.g. putting them into an Array as in your example.

'Within a normal module

Public Sub test()
    Dim Sw As New Device
    Dim Cap As New Device
    Dim Gen As New Device
    Dim Reg As New Device
    Dim Tr As New Device
    Dim Br As New Device

    DeviceArray = Array(Sw, Cap, Gen, Reg, Tr, Br)
    For i = LBound(DeviceArray) To UBound(DeviceArray)
       DeviceArray(i).value = 42
    Next i

    Debug.Print Gen.value
End Sub

'Output is 42

或者,根据您的需要,哈希映射也可以解决问题,在这种情况下,您将不声明变量,而是引用变量在映射中的条目.

Alternatively, depending on your needs, a hash map might also do the trick, in which case you would not declare variables but refer to their entries within the map.

Public Sub test2()
    Dim Devices As Object, DeviceArray
    Set Devices = CreateObject("Scripting.Dictionary")
    DeviceArray = Array("Sw", "Cap", "Gen", "Reg", "Tr", "Br")

    For i = 0 To UBound(DeviceArray)
        Devices(DeviceArray(i)) = 0
    Next i

    Debug.Print "Gen init to ", Devices("Gen")

    Devices("Gen") = 42

    Debug.Print "Gen is now ", Devices("Gen")

    For i = 0 To UBound(DeviceArray)
        Devices(DeviceArray(i)) = 0
    Next i

    Debug.Print "Gen reset to ", Devices("Gen")
End Sub


'Gen init to    0 
'Gen is now     42 
'Gen reset to   0 

这篇关于如何将数组中的值作为变量引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆