是否有可能在运行时更改VBA中的variables或函数的types?

我正在思考,如果有可能在enumtypes之间有条件地切换函数或variables的types。

像这样的东西:

Public Enum enmTest eA = 1 eB = 2 eC = 3 End Enum Public Enum enmDemo eA = 10 eB = 50 eC = 100 End Enum Public Function demoFunction() as enmDemo Dim eDemo as enmDemo ReDim eDemo as enmTest ReDim demoFunction as enmDemo End Function 'this does not work, but is there no way to make this work? Public Sub test() debug.print demoFunction().eA 'should be 1 End Sub 'this does not work, but is there no way to make this work? Public Sub test2 Dim temp as Variant temp = demoFunction() debug.print temp.eB 'should be 2 End Sub 

基本上,目标是有一个像Dim myVar这样的variables,它可能是enumAenumBtypes。 这些枚举可能是相同的,除了它们的值。

我的猜测是,这是不行的,因为VBA处理枚举的方式。 但只是为了确定我想得到一个解释,因为我经过一个小时的试验才有一种直觉。


我目前的解决方法,希望能够表明我的目标:

 Public Enum enmTest eA = 1 eB = 2 eC = 3 End Enum Public Enum enmDemo eA = 10 eB = 50 eC = 100 End Enum Public Function demo() Debug.Print Str(getValues(1)(1)) 'prints 1 Debug.Print Str(getValues(2)(1)) 'prints 10 End Function Public Function getArray(val1, val2, val3) as Variant Dim result as Variant ReDim result(1 to 3) result(1) = val1 result(2) = val2 result(3) = val3 getArray = result End Function Public Function getValues(myInt as Integer) as Variant If (myInt = 1) Then getValues = getArray(enmDemo.eA, enmDemo.eB, enmDemo.eC) Else getValues = getArray(enmTest.eA, enmTest.eB, enmTest.eC) End If End Function 

我可以提供的最好的是每个枚举types的自定义转换函数。 虽然我会回应丹斯评论:仔细考虑为什么你要这个。

 ' write one of these for each conversion you want Function CastToDemo(ByRef v As enmTest) As enmDemo Select Case v Case enmTest.eA CastToDemo = enmDemo.eA Case enmTest.eB CastToDemo = enmDemo.eB Case enmTest.eC CastToDemo = enmDemo.eC End Select End Function ' Use like this Public Sub test() Dim a As enmTest Dim b As enmDemo a = enmTest.eA b = CastToDemo(a) Debug.Print b End Sub 

我知道我们已经过了半年,但是如果有人发现这个…

你也可以通过类和接口(使用implements关键字)而不是枚举来实现你正在寻找的东西。 这比枚举更详细一些,但我认为它不像转换选项那样笨重。 如果你不得不使用枚举出于某种原因不包括在问题中,那么这并不能解决你的问题。 但是,如果你只是使用枚举作为具有数字值的命名variables的集合,那么这应该做的伎俩:

简而言之,您定义了一个接口(一个类),只有eA,eB和eC的公共只读成员。 这说明了每个可互换的“枚举”(类)必须具有的属性。

接口:

 ' In a class module called IEnm Public Property Get eA() As Long End Property Public Property Get eB() As Long End Property Public Property Get eC() As Long End Property 

然后你为每一个你想要的enumTest和enmDemo编写另一个类。 这些定义了每个属性的值。

enmTest:

 ' In a class module called enmTest Implements IEnm 'promises that this class defines each required property Public Property Get IEnm_eA() As Long IEnm_eA = 1 End Property Public Property Get IEnm_eB() As Long IEnm_eB = 2 End Property Public Property Get IEnm_eC() As Long IEnm_eC = 3 End Property 

enmDemo:

 ' In a class module called enmDemo Implements IEnm Public Property Get IEnm_eA() As Long IEnm_eA = 10 End Property Public Property Get IEnm_eB() As Long IEnm_eB = 50 End Property Public Property Get IEnm_eC() As Long IEnm_eC = 100 End Property 

这是一个演示如何使用它。

 Private actsLikeAnEnum As IEnm ' doesn't care if its enmTest, enmDemo, ' or enmSomethingElse Public Function demoFunction() As IEnm ' you don't know what you'll get out 'Dim eDemo As enmDemo 'ReDim eDemo as enmTest 'ReDim demoFunction as enmDemo Set actsLikeAnEnum = New enmTest Set demoFunction = actsLikeAnEnum ' you could just return a new enmTest, ' but I wanted to show that the single IEnm typed variable (actsLikeAnEnum) can ' store both enmTest type objects and enmDemo type objects End Function Public Sub test() Debug.Print demoFunction().eA 'prints 1 End Sub Public Sub test2() Dim temp As Variant ' since IEnm is an object, need to use the Set keyword Set temp = demoFunction() Debug.Print temp.eB 'prints 2 End Sub 'Or, if you want it to return 10 and 50.... Public Function demoFunctionTwo() As IEnm Set actsLikeAnEnum = New enmDemo Set demoFunctionTwo = actsLikeAnEnum End Function Public Sub test3() Debug.Print demoFunctionTwo().eA 'prints 10 End Sub Public Sub test4() Dim temp As Variant Set temp = demoFunctionTwo() Debug.Print temp.eB 'prints 50 End Sub 

您可以将actsLikeAnEnum(这是一个IEnmtypes对象)设置为新的enmDemo或enmTest,因为它们都实现了IEnm。 然后,您可以使用actsLikeAnEnum,而不必知道是否碰巧存在variables中的enmDemo对象或enmTest对象。