gpt4 book ai didi

vba - 类 "let"陷入无限循环

转载 作者:行者123 更新时间:2023-12-02 09:05:07 26 4
gpt4 key购买 nike

请原谅代码中的任何愚蠢错误,因为这是我第一次尝试使用类,而且我在网上找不到任何教程,可以为像我这样的傻瓜提供真正、真正简单的术语。我尽力遵循 MS 指南:https://msdn.microsoft.com/en-us/library/aa716315(v=vs.60).aspx ,但我真的不明白我在做什么,这使得很难解释我应该改变什么。

我正在尝试创建一个存储三段数据、两个整数和一个字符串的类。我将其放入名为 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和所有有趣的东西,一切都编译得很好,但是当它到达函数片段的第二行时,它尝试make t_info.t_acct 等于某个东西,它会转到该函数的 Let 函数,并永远留在那里。具体来说,它在

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

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

最佳答案

您的问题在这里:

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

这应该分配封装字段( tacct ),而不是它本身。

<小时/>

我将向您提供我的秘诀:每当我创建新的类模块时,我都会从私有(private)类型开始:

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

然后,我声明一个该类型的私有(private)字段,名为 this :

Private this As TData

有些人可能会认为 this让一切变得如此困惑,因为 this (私有(private)字段)不是 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成员(member)返回this.ThePropertyName ,和Property Let成员分配 this.ThePropertyName与提供的value - 总是。如果属性需要是非 get-only 的对象类型,则需要提供 Property Set成员(member):

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对于公共(public)成员,注重一致性,无论你做什么,都避免在公共(public)类成员的名字中使用下划线 - 否则你想开始使用 Implements 的那一天是你的代码停止编译的那一天。遵循这一点,您的类模块应该始终清晰可见。

关于vba - 类 "let"陷入无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42075141/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com