Reputation: 1
I'm getting just killed trying to make a class of a class. I have shopped around the site and seen several examples but maybe because its 1:43 I am having a hard time understanding them.
I was successfully able to use a class to automate a huge data entry project at work. I created a class called catDist which is the category distribution of types of agricultural products a company could manufacture or sell.
catDist contains six properties: Private selfWorth As String Private Q1 As Double Private Q2 as Double Private Q3 as Double Private Q4 As Double Private activated As Boolean
They all have the standard get and let codes.
There are 48 possible categories. I have a module that creates 48 instances of them with 48 different values for selfWorth (e.g "Cottonseed", or "maize" etc), and sets Q1 through Q4 as 0 . The module originally worked with a Userform that I could type in the values and hit enter. If it saw that I had entered a value inside a particular textbox (yes there were 48X4 textboxes) it would set activated to true and changes the relevant Q's to the values I entered.
WHAT I WANT TO DO NOW.
It was a great success. Now what I want to do is create a class called "Distributor". Each distributor class would have 4 collections have catDist objects. I can create the distributor class. I can create the catDist class. But for the love of God I can not figure out a way to set the corresponding distributor catDist property to the catDist value I used in the Set method.
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 Class
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
Registrant a.k.a distributor class
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
Upvotes: 0
Views: 77
Reputation: 3517
The only problem I see is that you are using Let
instead of Set
. In VBA you use Set
when assigning to objects.
When you write registrant.testCat = cowMilk
(in your Sub
), testCat = pCatDist
(in the getter of testRegistrant.testCat
) and pCatDist = cat
(in the setter of testRegistrant.testCat
) you are implicitly using Let
(it's as if you had written Let registrant.testCat = cowMilk
) instead of (explicitly) using Set
.
So, if you write Set registrant.testCat = cowMilk
in your test Sub
, Set testCat = pCatDist
in the getter and Set pCatDist = cat
in the setter you should be good to go.
Also, in the same setter, the initialization of pCatDist
isn't needed since you are passing cat
to it in the next line.
And, as @GSerg (thank you) says, the signature of your setter should be Public Property Set testCat(cat as testcatDist)
instead of Public Property Let
.
Upvotes: 2