在Excel VBA中从类到接口的转换

在Excel 2013中,我有两个类: LoadCaseLoadCombination ,实现接口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 

LoadCaseLoadCombination的(部分)实现是:

 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是一个可以同时包含LoadCaseLoadCombination对象的集合。

问题发生在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 ,但我不明白为什么。 这可能是问题的原因吗?