2014-10-12 28 views
0

我刚刚试图做一个班的类。我在网站上逛过,看过几个例子,但也许因为1:43我很难理解它们。一类的课

我成功地使用一个类来自动化一个巨大的数据录入项目。我创建了一个名为catDist的类,它是一家公司可以生产或销售的农产品类型的分类分布。

catDist包含六个属性: 私人selfWorth作为字符串 私人Q1为双 私人Q2为双 私人Q3为双 私人Q4为双 私人激活为布尔

他们都有标准得到和让代码。

有48种可能的类别。我有一个模块可以为selfWorth(例如“Cottonseed”或“maize”等)创建48个不同值的实例,并将Q1至Q4设置为0。该模块最初与一个用户窗体一起工作,我可以输入值并按回车键。如果它看到我在一个特定的文本框中输入了一个值(是的,那里有48X4个文本框),它将被设置为true并将相关的Q值更改为我输入的值。

我现在想做什么。

这是一个巨大的成功。现在我想要做的是创建一个名为“分销商”的类。每个分销商类将有4个集合拥有catDist对象。我可以创建分销商类。我可以创建catDist类。但是对于上帝的爱,我无法想出将相应分销商的CatDist属性设置为我在Set方法中使用的catDist值的方法。

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 Sub 

catDist类

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 

注册人a.k.a经销商类

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 
+0

请显示一些代码:) – 2014-10-12 05:56:48

+0

哈哈我试图避免这一点。我会把我用来试验这个概念的一个简化版本。 – 2014-10-12 05:57:25

+0

如果您阅读了此处发布的最佳做法,请参阅您希望发布一个简短的自描述正确的代码示例。原因是,如果我们能够理解具体问题,帮助你更容易 – 2014-10-12 05:59:23

回答

2

我看到的唯一问题是,你正在使用Let代替Set。在VBA中,分配给对象时使用Set

当你testCat = pCatDist(在testRegistrant.testCat吸气)和pCatDist = cat(在testRegistrant.testCat二传手),你都隐含使用Letregistrant.testCat = cowMilk(在你的Sub)(就好像你写Let registrant.testCat = cowMilk),而不是(明确)使用Set

所以,如果你在你的测试SubSet testCat = pCatDist在getter和Set pCatDist = cat在二传手写Set registrant.testCat = cowMilk你要善于去。

另外,在同一个setter中,不需要初始化pCatDist,因为您在下一行中将cat传递给它。

而且,正如@GSerg(谢谢)所说,您的setter的签名应该是Public Property Set testCat(cat as testcatDist)而不是Public Property Let

+3

不仅如此,还可以用'Public Property Set testCat'替换'Public Property Let testCat'。 – GSerg 2014-10-12 09:36:25