3

我第一次尝试在 VBA 中创建一个类。我查找了一些解决方案,我的类没有发现任何问题,但是当我运行测试代码时,整数返回正确,但字符串返回空:

班级

Property Let Name(strName As String)
    pName = strName
End Property
Property Get Name() As String
    Name = pName
End Property
Property Let Class(strClass As String)
    pClass = strClass
End Property
Property Get Class() As String
    Class = pClass
End Property
Property Let Aggro(intAggro As Integer)
    pAggro = intAggro
End Property
Property Get Aggro() As Integer
    Aggro = pAggro
End Property

测试程序

Sub ClassTest()
    Dim Dog1 As New Critter

    Dog1.Name = "Labrador"
    Dog1.Class = "Canine"
    Dog1.Aggro = 0     

    Debug.Print Dog1.Name  'returns ""
    Debug.Print Dog1.Class  'returns ""
    Debug.Print Dog1.Aggro  'returns 0  
End Sub
4

1 回答 1

4

你唯一的错误是你没有定义私有变量来保存你的属性值。看起来整数正在工作,因为整数初始化为 0,并且您正在将值“设置”为 0。只需将其添加到类的顶部,然后重试:

Private pName as String
Private pClass as String
Private pAggro as Integer

:D

于 2013-04-19T00:53:59.950 回答