编译错误:预期的函数或变量

问题描述 投票:0回答:1

我有一个名为

IHuman
的接口,并且有 2 个类实现它,即
cMan
cWoman
。我有一个
Test
模块来测试代码,如下所示:

Test
模块

Option Explicit

Sub Test()

Dim Man As IHuman
Dim Woman As IHuman

Set Man = New cMan
Debug.Print Man.ManMessage
Debug.Print Man.WomanMessage

Set Woman = New cWoman
Debug.Print Woman.WomanMessage
Debug.Print Woman.ManMessage

End Sub

IHuman
界面

Option Explicit

Public Sub ManMessage()
End Sub

Public Sub WomanMessage()
End Sub

cMan
班级

Option Explicit

Implements IHuman

Private Sub IHuman_ManMessage()
    Debug.Print "I am a Man."
End Sub

Private Sub IHuman_WomanMessage()
    Debug.Print "I am NOT a Woman."
End Sub

cWoman
班级

Option Explicit

Implements IHuman

Private Sub IHuman_ManMessage()
    Debug.Print "I am NOT a Man."
End Sub

Private Sub IHuman_WomanMessage()
    Debug.Print "I am a Woman."
End Sub

运行

Test
模块时,我收到以下错误:

Compile error: Expected Function or variable

Debug.Print Man.ManMessage
模块中的
Test
线上。

我使用的是 Excel 365 for Mac 最新版本。知道可能是什么问题吗?

excel vba class interface
1个回答
0
投票

接口最好用作方法的参数。

cMan课程

Option Explicit

Implements IHuman

Private Sub IHuman_Message()
    Debug.Print "I am a Man."
End Sub

c女人课

Option Explicit

Implements IHuman

Private Sub IHuman_Message()
    Debug.Print "I am a Woman."
End Sub

I人机界面

Option Explicit

Public Sub Message()
End Sub

测试模块

Option Explicit

Sub Test()

Dim Person1 As cMan
Dim Person2 As cWoman

Set Person1 = New cMan
Set Person2 = New cWoman

reportGender Person1
reportGender Person2

End Sub

Sub ReportGenber(byref ipHuman as IHuman)
    debug.print ipHuman.Message
end sub
© www.soinside.com 2019 - 2024. All rights reserved.