我想第一次在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
答案 0 :(得分:4)
唯一错误的是您没有定义私有变量来保存属性值。似乎整数正在工作,因为Integer初始化为0,并且您将该值设置为0.只需将其添加到类的顶部并重试:
Private pName as String
Private pClass as String
Private pAggro as Integer
:d