VBA:使用变量作为用户定义类型的引用

时间:2022-11-05 08:40:04

How can I reference a user defined type using a local variable without creating a copy of the type instance?

如何在不创建类型实例副本的情况下使用局部变量引用用户定义的类型?

As an example, in the code below what I would ideally like to do is in MySub3 where I create a local variable, MT, and reference a data structure nested inside another struct ... but VBA doesn't allow this. It allows it for objects but not for user defined types (arrggg!) ... and for no apparent reason ... it just doesn't allow it.

作为一个例子,在下面的代码中,我理想的是在MySub3中我创建一个局部变量MT,并引用嵌套在另一个结构中的数据结构...但是VBA不允许这样做。它允许它用于对象,但不允许用户定义的类型(arrggg!)......并且没有明显的原因......它只是不允许它。

MySub1 shows how to reference the nested struct in a long clunky way. MySub2 shows how to do this by passing in the nested struct, but this clutters up the calling routine, and having multiple such nested structs gets ugly.

MySub1展示了如何以长而笨重的方式引用嵌套结构。 MySub2显示了如何通过传入嵌套结构来实现这一点,但这会使调用例程变得混乱,并且有多个这样的嵌套结构变得丑陋。

MySub2 demonstrates that VBA can do what I want, it just doesn't seem to provide a way to do it. I'm hoping there is a method I just haven't stumbled upon.

MySub2表明VBA可以做我想做的事情,它似乎没有提供一种方法来做到这一点。我希望有一种我没有偶然发现的方法。

Note that my actual code is MUCH more complicated than this example, with multiple independent structs providing indices to many arrays as struct elements. Using these local reference variables would make the code much more readable and manageable.

请注意,我的实际代码比此示例复杂得多,多个独立的结构为许多数组提供索引作为结构元素。使用这些本地引用变量将使代码更具可读性和可管理性。

Also Note that I am aware of the "with" statement, and it does help, but can only be used on one struct at a time.

另请注意,我知道“with”语句,它确实有帮助,但一次只能在一个结构上使用。

Also Note that I am aware that I could use an actual object class. My code started out using an object but I quickly found out that VBA places limitations on arrays as property members ... a limitation that user defined types don't have.

另请注意,我知道我可以使用实际的对象类。我的代码开始使用一个对象,但我很快发现VBA将数组的限制作为属性成员......用户定义的类型没有的限制。

Type tMyType
    VariableA As Single
End Type

Type tMyOtherType
    MyTypeArray() As tMyType
End Type

Type tOneMoreType
    MyOtherType As tMyOtherType
End Type

Dim GlobalIndex As Integer

Sub TopLevel()
    Dim TopLevelType As tOneMoreType

    ReDim TopLevelType.MyOtherType.MyTypeArray(0 To 10)
    Call MySub1(TopLevelType)
    Call MySub2(TopLevelType.MyOtherType.MyTypeArray(GlobalIndex))
    Call MySub3(TopLevelType)
End Sub

Sub MySub1(OMT As tOneMoreType)
    Dim VarA As Single

    VarA = OMT.MyOtherType.MyTypeArray(GlobalIndex).VariableA
End Sub

Sub MySub2(MT As tMyType)
    Dim VarA As Single

    VarA = MT.VariableA
End Sub

Sub MySub3(OMT As tOneMoreType)
    Dim VarA As Single
    Dim MT

    Set MT = OMT.MyOtherType.MyTypeArray(GlobalIndex)
    VarA = MT.VariableA
End Sub

2 个解决方案

#1


0  

From my point of view you have made it vary complicated. But I believe you have the reason for that. The example you submitted generate the error you mentioned. But, when I changed some lines there is no error. I am not sure if my suggestion is the result you expected (while the question isn't fully clear to me) but try this instead of your MySub3:

从我的角度来看,你已经变得复杂了。但我相信你有理由这样做。您提交的示例会生成您提到的错误。但是,当我改变一些行时,没有错误。我不确定我的建议是否是你期望的结果(虽然这个问题对我来说并不完全清楚)但是请尝试这个而不是你的MySub3:

Sub MySub3(OMT As tOneMoreType)
Dim VarA As Single
Dim MT

MT = OMT.MyOtherType.MyTypeArray(GlobalIndex).VariableA

VarA = MT

End Sub

Generally, this way I'm able to read any element im MySub3 passed from TopLevel. If it is not the answer please clarify more.

通常,这样我就可以读取从TopLevel传递的任何元素im MySub3。如果不是答案,请澄清更多。

#2


0  

I think here you have hit one of the limitations of VBA. I know of no way round the limitation on partial dereferencing of nested user types.

我想在这里你遇到了VBA的一个局限性。我知道无法绕过嵌套用户类型的部分解引用限制。

I think you would be best using classes containing private arrays with getter and setter functions (sadly, VBA doesn't have operator overloading either).

我认为你最好使用包含带有getter和setter函数的私有数组的类(遗憾的是,VBA也没有运算符重载)。

#1


0  

From my point of view you have made it vary complicated. But I believe you have the reason for that. The example you submitted generate the error you mentioned. But, when I changed some lines there is no error. I am not sure if my suggestion is the result you expected (while the question isn't fully clear to me) but try this instead of your MySub3:

从我的角度来看,你已经变得复杂了。但我相信你有理由这样做。您提交的示例会生成您提到的错误。但是,当我改变一些行时,没有错误。我不确定我的建议是否是你期望的结果(虽然这个问题对我来说并不完全清楚)但是请尝试这个而不是你的MySub3:

Sub MySub3(OMT As tOneMoreType)
Dim VarA As Single
Dim MT

MT = OMT.MyOtherType.MyTypeArray(GlobalIndex).VariableA

VarA = MT

End Sub

Generally, this way I'm able to read any element im MySub3 passed from TopLevel. If it is not the answer please clarify more.

通常,这样我就可以读取从TopLevel传递的任何元素im MySub3。如果不是答案,请澄清更多。

#2


0  

I think here you have hit one of the limitations of VBA. I know of no way round the limitation on partial dereferencing of nested user types.

我想在这里你遇到了VBA的一个局限性。我知道无法绕过嵌套用户类型的部分解引用限制。

I think you would be best using classes containing private arrays with getter and setter functions (sadly, VBA doesn't have operator overloading either).

我认为你最好使用包含带有getter和setter函数的私有数组的类(遗憾的是,VBA也没有运算符重载)。