我只是被杀了,试图使一个类。我在这个网站上逛了逛,看到了几个例子,但可能是因为现在是1:43,我很难理解它们。
在工作中,我成功地使用了一个类来自动化一个大型数据输入项目。我创建了一个名为catDist的类,它是公司可以生产或销售的农产品类型的类别分布。
catDist包含六个属性:专用selfWorth As字符串专用Q1 As双重专用Q2 as双重专用Q3 as双重专用Q4 As双重专用激活为布尔值
它们都有标准的get和let代码。
有48个可能的类别。我有一个模块,它用48个不同的selfWorth值(例如“棉籽”或“玉米”等)创建48个实例,并将Q1到Q4设置为0。该模块最初使用的是Userform,我可以输入值并按enter键。如果它看到我在一个特定的文本框中输入了一个值(是的,有48X4个文本框),它会将激活设置为true,并将相关的Q更改为我输入的值。
我现在想做的是。
这是一个巨大的成功。现在我要做的是创建一个名为"Distributor“的类。每个分发服务器类将有4个具有catDist对象的集合。我可以创建分发服务器类。我可以创建catDist类。但是看在上帝的份上,我想不出一种方法来将相应的分发服务器catDist属性设置为我在catDist方法中使用的set值。
Sub testRegist()
Dim registrant As testRegistrant
Set registrant = New testRegistrant
registrant.registNum = "Z000123"
'MsgBox (registrant.registNum)
Dim cowMilk As testcatDist
Set cowMilk = New testcatDist
cowMilk.selfWorth = "Cow Milk"
cowMilk.distribution = 4.6
registrant.testCat = cowMilk
Debug.Print registrant.testCat.selfWorth
End SubcatDist类
Private pselfWorth As String
Private pdistribution As Double
Public Property Get selfWorth() As String
selfWorth = pselfWorth
End Property
Public Property Let selfWorth(name As String)
pselfWorth = name
End Property
Public Property Get distribution() As Double
distribution = pdistribution
End Property
Public Property Let distribution(dist As Double)
pdistribution = dist
End Property注册者又称分发者类
Private pRegistNum As String
Private pCatDist As testcatDist
Public Property Get registNum() As String
registNum = pRegistNum
End Property
Public Property Let registNum(registration As String)
pRegistNum = registration
End Property
Public Property Get testCat() As testcatDist
testCat = pCatDist
End Property
Public Property Let testCat(cat As testcatDist)
Set pCatDist = New testcatDist
pCatDist = cat
End Property发布于 2014-10-12 17:30:27
我看到的唯一问题是您使用的是Let而不是Set。在VBA中,为对象赋值时使用Set。
当您编写registrant.testCat = cowMilk (在Sub中)、testCat = pCatDist (在testRegistrant.testCat的getter中)和pCatDist = cat (在testRegistrant.testCat的setter中)时,您隐式地使用了Let (就像您已经编写了Let registrant.testCat = cowMilk一样),而不是(显式地)使用Set。
因此,如果您在测试Sub中编写Set registrant.testCat = cowMilk,在getter中编写Set testCat = pCatDist,在setter中编写Set pCatDist = cat,就应该可以了。
此外,在同一个setter中,不需要初始化pCatDist,因为您将在下一行中将cat传递给它。
而且,正如@GSerg (谢谢你)所说,你的setter的签名应该是Public Property Set testCat(cat as testcatDist)而不是Public Property Let。
https://stackoverflow.com/questions/26322323
复制相似问题