在Excel VBA中从类到接口的转换
在Excel 2013中,我有两个类: LoadCase
和LoadCombination
,实现接口ILoadCase
。
ILoadCase
的声明是:
Option Explicit '' Public properties Public Property Get Name() As String End Property Public Property Let Name(ByVal value As String) End Property Public Property Get ID() As Long End Property Public Property Let ID(ByVal valus As Long) End Property
LoadCase
和LoadCombination
的(部分)实现是:
Option Explicit Implements ILoadCase '' Public properties Public Property Get ILoadCase_Name() As String ILoadCase_Name = pName End Property Private Property Let ILoadCase_Name(ByVal value As String) pName = value End Property Public Property Get ILoadCase_ID() As Long ILoadCase_ID = pID End Property Private Property Let ILoadCase_ID(ByVal value As Long) pID = value End Property
我省略了与接口实现无关的代码。
然后我有一个BeamForces
类,其中包含特定ILoadCase
对象的结果:
Option Explicit Public Fx As Double Public Fy As Double Public Fz As Double Public Mx As Double Public My As Double Public Mz As Double Public ParentLoadCase As ILoadCase
我认为这样做我可以做这样的事情:
Set currentBeamForces = New BeamForces With currentBeamForces .Fx = forces(0) .Fy = forces(1) .Fz = forces(2) .Mx = forces(3) .My = forces(4) .Mz = forces(5) Set .ParentLoadCase = TargetLoadCase End With
TargetLoadCase
是一个LoadCase
或一个LoadCombination
,但每次都会给我一个错误。
我已经编写了这个像我在.NET中,只是预计它会工作,但没有铸造到一个接口不能在VBA工作? 还是我在这里错了?
编辑
更多细节。 我首先调用下面的方法:
Public Function LoadBeamForcesAtNode(ByVal TargetBeam As Beam, ByVal TargetNode As Node, Optional ByVal TargetLoadCases As Collection = Nothing) As Boolean Dim i As Integer Dim currentLoadCase As Variant Dim targetBeamForces As BeamForces If TargetLoadCases Is Nothing Then For Each currentLoadCase In Me.LoadCases.Items Call TargetLoadCases.Add(currentLoadCase) Next For Each currentLoadCase In Me.LoadCombinations.Items Call TargetLoadCases.Add(currentLoadCase) Next End If 'On Error GoTo ExitPoint For Each currentLoadCase In TargetLoadCases Set targetBeamForces = InstantiateBeamForces(TargetBeam, TargetNode, currentLoadCase) If TargetNode Is TargetBeam.Node1 Then Set TargetBeam.Forces1 = targetBeamForces Else Set TargetBeam.Forces2 = targetBeamForces End If Next LoadBeamForcesAtNode = True ExitPoint: End Function
TargetLoadCases
是一个可以同时包含LoadCase
和LoadCombination
对象的集合。
问题发生在InstantiateBeamForces
,它的代码是
Private Function InstantiateBeamForces(ByVal TargetBeam As Beam, ByVal TargetNode As Node, ByVal TargetLoadCase As Variant) As BeamForces Dim forces(5) As Double Dim currentBeamForces As BeamForces Call Me.output.GetMemberEndForces(TargetBeam.ID, IIf(TargetNode Is TargetBeam.Node1, 0, 1), TargetLoadCase.ILoadCase_ID, forces, 0) Set currentBeamForces = New BeamForces With currentBeamForces .Fx = forces(0) .Fy = forces(1) .Fz = forces(2) .Mx = forces(3) .My = forces(4) .Mz = forces(5) Set .ParentLoadCase = TargetLoadCase End With Set InstantiateBeamForces = currentBeamForces End Function
其中创build一个新的BeamForces
对象,并填充返回的值...GetMemberEndForces(...)
API COM调用。 问题是.ParentLoadCase
属性没有任何分配后,所以我假设一个无效的转换…
**编辑2 **
这里是我在InstantiateBeamForces
放置一个断点时的TargetLoadCase
截图。
ILoadCase
成员是Nothing
,但我不明白为什么。 这可能是问题的原因吗?