Class on a class

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a variable, Var1, which is of type Class2.

Standard Module:


Code:
Option Explicit


Sub Test()

    Dim a As Class1
    Set a = New Class1
    
    a.Var1.Var2 = 7678

End Sub


Class1

Code:
Option Explicit


    Private pVar1 As New Class2


Public Property Get Var1() As Class2


    Set Var1 = pVar1
    
End Property


Public Property Set Var1(ByVal vNewValue As Class2)


    Set pVar1 = vNewValue
    
End Property


Private Sub Class_Initialize()


    Var1.Var2 = 336
    
End Sub


Class2

Code:
Option Explicit


    Private pVar2 As String


Public Property Get Var2() As String


    Var2 = pVar2
    
End Property


Public Property Let Var2(ByVal vNewValue As String)


    pVar2 = vNewValue
    
End Property


The code above works.

We use Let for "normal" variables and Set for object variables.

What I don't understand is this line:

Code:
Private Sub Class_Initialize()


    Var1.Var2 = 336
    
End Sub


Since Var1 is of type Var2, I expected to have to write:

Code:
Private Sub Class_Initialize()


    SET Var1.Var2 = 336
    
End Sub


Why don't I need to SET keyword here?

Is it because of this line at the top of Class1?

Code:
    Private pVar1 As New Class2



Thanks
 
Last edited:

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Var1.Var2 = 336

Why don't I need to SET keyword here?

.. because you're setting the Var2 property (which is a string) of the existing Var1 object.

Just by the way, it's better to avoid auto-instancing, e.g. Private pVar1 As New Class2
See Chip Pearson's comments, for example:
http://www.cpearson.com/excel/classes.aspx
http://www.cpearson.com/excel/declaringvariables.aspx

Instead:

Code:
'In Class1
Private pVar1 As Class2
Private Sub Class_Initialize()

    Set pVar1 = New Class2
    pVar1.Var2 = "336"    'for your testing purposes
    
End Sub
 
Upvote 0
Thanks for the PM ...

No, I wouldn't call this inheritance .. you've just defined Class1 as having a property that is a Class2 object. That doesn't mean that Class2 will inherit any other properties or methods of Class1 that you may choose to define.

VBA doesn't support direct inheritance, but you can use class interfaces, e.g. http://www.cpearson.com/excel/Implements.aspx
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,557
Members
449,088
Latest member
davidcom

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