Classes and objects

Hap

Well-known Member
Joined
Jul 20, 2005
Messages
647
Can an object be a parent to another object in a class module? I tried setting up my class to have a property of another object type but I get an Object Variable not set error. What is the correct syntax for this? My code reads something like this:

Class Named "Structure"

Option Explicit

Private cSize As Size

Public Property Get Size() As Size
Size = cSize
End Property

"Size" is another class module with properties like "Length", "Width", "Height", etc.

I would like to contain all the dimensional information within the "Size" class and only refer to it in the "Structure" class.
 
for reference types (like your own classes) you cannot use Let in the property setter, like this:
Code:
Public Property Let NodeStart(Value As Node)
cNodeStart = Value
End Property
You have to use it like this:
Code:
Public Property [COLOR=red][B]Set[/B][/COLOR] NodeStart(Value As Node)
[COLOR=red][B]Set[/B][/COLOR] cNodeStart = Value
End Property

Also this
Code:
NodeStart = cNodeStart
should be this:
Code:
[COLOR=red][B]Set[/B][/COLOR] NodeStart = cNodeStart
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
That worked. I guess I'm not understanding something here.

Use the Let statement when directly assigning a value to a property (which is basically an object variable)? You can still use the Let statement if you are using the ByRef statement for a specific variable or property though correct?

Use the Set statement when referencing an object? How do you know to use the Set statement inside the Set reference? Is it the introduction of a new reference variable?

Thank you for your help. Where could a guy find some good reference material regarding this online?
 
Upvote 0
I have not yet found a good online resource about classes and objects for VBA. Chip Pearson's site (google him, great resource!) has a few things, but not very exhaustive...

Not sure I understand your questions fully... let me try to explain:
- there are value and reference types in variables. Value types are the built-in types like integer, double, boolean, string (special case, but for now, just treat it as a value type). Reference types are instances of classes, like worksheets, workbooks, ranges, all your own classes, etcetera.
- value types get their values assigned with Let. In most cases you don't have to type this in your code, but when you have something like:
Code:
Dim i as Integer
i = 20
You are actually saying:
Code:
Dim i as Integer
Let i = 20
But for those cases the compiler understands what you want and implicitly adds the Let keyword itself.
- reference types always need to have the Set keyword when being assigned values.
- So, for a property setter, you need to use Let for value type properties and Set for reference type properties. You cannot use Set for value type properties, nor can you use Let for reference type properties...
- In the following snippet:
Code:
Public Property Set NodeStart(Value As Node)
Set cNodeStart = Value
End Property
The first Set is needed because you are having a reference type property, the second Set is needed because you are assigning a value to a reference variable.

In modern VB(.NET) the compiler is smart enough to figure out the difference between value and reference types and you don't need to use Let or Set anymore.

I hope that makes things perfectly clear now :biggrin:
 
Upvote 0

Forum statistics

Threads
1,225,156
Messages
6,183,227
Members
453,152
Latest member
ChrisMd

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top