Class" let"陷入无限循环

时间:2017-02-06 18:53:42

标签: vba excel-vba excel

原谅代码中的任何愚蠢错误,因为这是我第一次尝试使用类,我无法找到任何在线教程,真正用真正简单的术语来表示傻瓜我。我尽力按照https://msdn.microsoft.com/en-us/library/aa716315(v=vs.60).aspx的MS指南进行操作,但我真的不明白我在做什么,这使我很难解释我应该改变的内容。

我正在尝试创建一个存储三个数据,两个整数和一个字符串的类。我把它放在一个名为tdata的类模块中:

Sub tdata()

Dim tnumber As Integer, tacct As Integer
Dim ttype As String

Public Property Get t_acct() As Integer         'don't forget the account number!
    t_acct = tacct
End Property

Public Property Let t_acct(ByVal newval As Integer)
    t_acct = newval
End Property

Public Property Get t_numb() As Integer         'T1, T2, or T3 as applicable
    t_numb = tnumb
End Property

Public Property Let t_numb(ByVal newval As Integer)
    t_numb = newval
End Property

Public Property Get t_type() As String          'PF or MW
    t_type = ttype
End Property

Public Property Let t_type(ByVal newstr As String)
    t_type = newstr
End Property

End Sub

然后我使用

在我的函数中调用它
Set t_info = New tdata
t_info.t_acct = wb2.Sheets(1).Cells(d, 1)             'd is just a row counter in a for loop
t_info.t_numb = Right(wb2.Sheets(1).Cells(d, 4), 1)
t_info.t_type = wb2.Sheets(1).Cells(d, 6)
references(CStr(wb2.Sheets(1).Cells(d, 5))).Add t_info

(当然,这不是所有代码,只是调用它的部分)

我已经获得了Option Explicit以及所有有趣的内容并且所有内容都编译得很好,但是当它到达功能代码段的第二行时,它会尝试使t_info.t_acct等于某事,它前往那个Let函数,并永远留在那里......具体来说,它在

之间反弹
Public Property Let t_acct(ByVal newval As Integer)
    t_acct = newval

永远。为什么是这样?如何设置(错误,让)t_acct等于我想要的东西?

1 个答案:

答案 0 :(得分:12)

您的问题在这里:

Public Property Let t_acct(ByVal newval As Integer)
    t_acct = newval
End Property

那应该是分配封装的字段(tacct),而不是它自己。

我将给你我的秘方:每当我创建一个新的类模块时,我都会以私有类型开头:

Option Explicit
Private Type TData 'assuming class module is named 'Data'
    Number As Integer
    Account As Integer
    AccountType As String
End Type

然后,我声明了一个名为this

的私有字段
Private this As TData

有些人可能认为this会让一切变得如此混乱,因为this(私有字段)不是Me(对象实例),而是this指对象实例和诸如此类的东西 - 如果它让你感到困惑,那就给它你喜欢的任何名字(backingencapsulated也完全没问题!)。

现在所有属性都变得清晰,一致:

Public Property Get Number() As Integer
    Number = this.Number
End Property

Public Property Let Number(ByVal value As Integer)
    this.Number = value
End Property

Public Property Get Account() As Integer
    Account = this.Account
End Property

Public Property Let Account(ByVal value As Integer)
    this.Account = value
End Property

Public Property Get AccountType() As String
    AccountType = this.AccountType
End Property

Public Property Let AccountType(ByVal value As String)
    this.AccountType = value
End Property

Property Get成员返回this.ThePropertyNameProperty Let成员使用提供的this.ThePropertyName分配 value - 始终。如果属性需要是非get-only的对象类型,那么您将需要提供Property Set成员:

Private Type TData
    '...
    SomeObject As Object
End Type
Private this As TData    

Public Property Get SomeObject() As Object
    Set SomeObject = this.SomeObject
End Property

Public Property Set SomeObject(ByVal value As Object)
    Set this.SomeObject = value
End Property

避免贬义,前缀和不可读/无意义的名称,对公共成员使用PascalCase,支持一致性,无论你做什么,都要避免公共类成员姓名中的下划线 - 否则就要开始使用{{ 1}}是代码停止编译的那一天。接下来,您的课程模块应该始终清晰。