问题描述
因为这是我第一次使用类的尝试,因此我无法找到任何在线的教程,将其放在真正的呃,像我这样的傻瓜。我尽力按照,但我真的不明白我在做什么,这使得它很难解释我应该改变什么我正在尝试使一个类存储三个数据,两个整数和一个字符串。我将它放在一个类模块中,名为 tdata
:
Sub tdata )
pre>
Dim tnumber As Integer,tacct As Integer
Dim ttype As String
公共属性获取t_acct()As Integer'不要忘记帐号!
t_acct = tacct
结束属性
公共属性让t_acct(ByVal newval As Integer)
t_acct = newval
结束属性
公共属性获取t_numb()As Integer'T1,T2或T3(如适用)
t_numb = tnumb
结束属性
公共属性让t_numb(ByVal newval As Integer)
t_numb = newval
结束属性
公共属性获取t_type()As String'PF或MW
t_type = ttype
结束属性
公共属性让t_type(ByVal newstr As String)
t_type = newstr
结束属性
End Sub
然后我使用
设置t_info = new tdata
t_info.t_acct = wb2.Sheets(1).Cells(d,1)'d只是for循环中的行计数器
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 ,5))) d t_info
(这不是所有的代码,当然,只是它被称为的部分)
我有
Option Explicit
,所有有趣的东西和一切编辑都很好,但是当它得到到函数代码段的第二行,它尝试使t_info.t_acct
等于某事,它将转到让
为那个功能,永远留在那里。具体来说,它会在
$ pre>公共属性之间弹跳t_acct(ByVal newval As Integer)
t_acct = newval
永远不会。/ / code>
为什么是这样?如何让它设置(err,let)t_acct等于我想要的东西?
您的问题在此:
应该分配封装的字段( tacct
),而不是本身。 p>
我将给你我的秘方:每当我创建一个新的类模块,我从一个私人类型开始:
Option Explicit
私有类型TData'假设类模块命名为'数据'
数字为整数
帐户为整数
AccountType As String
结束类型
然后,我声明一个这样的私人字段,名为这个
:
私人这个作为TData
有些人可能会认为这个
这个(p rivate字段)不是我
(对象实例)和其他语言这个
是指对象实例和whatnot - 如果它混淆你,给它任何你喜欢的名字,然后(支持
和封装
也是非常好的!
现在,所有的属性都变得清晰,一致:
Number = this.Number
End Property
公共属性让数字(ByVal值为整数)
this.Number =值
结束属性
公共属性获取帐户()作为整数
帐户= this.Account
结束属性
公共财产让帐户(ByVal值As Integer)
this.Account = value
结束属性
公共属性获取AccountType()As String
AccountType = this.AccountType
结束属性
公共属性让AccountType(ByVal值As String)
this.AccountType = value
结束属性
Property Get
成员返回 this.ThePropertyName
和 Property Let
成员分配 this.ThePropertyName
与提供的值
- 永远。如果一个属性需要一个不是唯一的对象类型,那么你需要提供一个属性集
成员:
私人类型TData
'...
SomeObject作为对象
结束类型
私有它作为TData
公共属性Get SomeObject()As Object
Set SomeObject = this.SomeObject
结束属性
公共属性Set SomeObject(ByVal value As Object)
设置this.SomeObject =值
结束属性
避免使用disomvoweling,前缀和不可读/无意义的名称,为公共成员使用 PascalCase
,有利于一致性,无论你做什么,都可以避免在公共类成员的名字中加下划线 - 否则您要开始使用的日子实现
是您的代码停止编译的那一天。遵循这一点,您的课程模块应该始终保持清晰。
Forgive any stupid errors in the code, because this is my first attempt at using classes and I couldn't find any tutorials online that put it in really, really plain terms for fools like me. I did my best to sort of follow the MS guide at https://msdn.microsoft.com/en-us/library/aa716315(v=vs.60).aspx, but I don't really understand what I'm doing anyway, which makes it hard to interpret what I should be changing.
I am trying to make a class that stores three pieces of data, two integers and a string. I put this in a class module called 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
I then called it in my function using
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
(this is not all the code, of course, but just the part where it is called)
I've got Option Explicit
and all that fun stuff on and everything compiles fine, but when it gets to the second line of the function snippet, where it tries make t_info.t_acct
equal to something, it heads to the Let
function for that one, and stays there...forever. Specifically, it bounces between
Public Property Let t_acct(ByVal newval As Integer)
t_acct = newval
forever. Why is this? How do I make it set (err, let) t_acct equal the thing I want it to?
Your issue is here:
That should be assigning the encapsulated field (tacct
), not itself.
I'm going to give you my secret recipe: whenever I create a new class module, I start with a private type:
Option Explicit
Private Type TData 'assuming class module is named 'Data'
Number As Integer
Account As Integer
AccountType As String
End Type
And then, I declare a private field of that type, named this
:
Private this As TData
Some might argue that this
makes everything oh so confusing because this
(the private field) isn't Me
(the object instance) and in other languages this
refers to the object instance and whatnot - if it confuses you, give it whatever name you like then (backing
and encapsulated
are perfectly fine too!).
Now all the properties become crystal-clear, and consistent:
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
members return this.ThePropertyName
, and Property Let
members assign this.ThePropertyName
with the supplied value
- always. If a property needs to be an object type that isn't get-only, you'll want to supply a 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
Avoid disemvoweling, prefixes and unreadable/meaningless names, use PascalCase
for public members, favor consistency, and whatever you do, avoid underscores in public class members' names - otherwise the day you want to start using Implements
is the day your code stops compiling. Follow that, and your class modules should always be crystal-clear.
这篇关于类“let”陷入无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!