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.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I'm thinking maybe I'm missing something in trying to keep the naming convention the same. The question I guess is can a property be an object? If not, how do you declare the use of the object?
 
Upvote 0
I would like to contain all the dimensional information within the "Size" class and only refer to it in the "Structure" class
You can't do this as VB doesn't support inheritance. However, with a bit of trickery, I seem to have managed to fake this functionality and worked just fine when tested .

Two Classes :

Class C_Person has a Name Property and a Size Property.
Class C_Size has a Height Property and a Weight Property.

The Size Property of the C_Person Class returns a C_Size Object .


C_Person Class :
Code:
Option Explicit

Private oSize As C_Size
Private sName As String
Private lCounter As Long

Public Property Get Name() As String
    Name = sName
End Property

Public Property Let Name(ByVal vNewValue As String)
    sName = vNewValue
End Property

Public Property Get Size() As C_Size

    If lCounter = 0 Then
        lCounter = lCounter + 1
        Set oSize = New C_Size
    End If
    Set Size = oSize

End Property
C_Size Class :
Code:
Option Explicit

Private W As Single
Private h As Single

Public Property Get Weight() As Single
    Weight = W
End Property

Public Property Let Weight(ByVal vNewValue As Single)
    W = vNewValue
End Property

Public Property Get Height() As Single
    Height = h
End Property

Public Property Let Height(ByVal vNewValue As Single)
    h = vNewValue
End Property
Usage code :

Code:
Option Explicit

Sub Test()

    Dim oPers1 As C_Person
    Dim oPers2 As C_Person

    Set oPers1 = New C_Person
    Set oPers2 = New C_Person
    
    With oPers1
        .Name = "Albert"
        .Size.Height = 184
        .Size.Weight = 90
    End With
    
    With oPers2
        .Name = "Claire"
        .Size.Height = 172
        .Size.Weight = 75
    End With
    
    With oPers1
        MsgBox "'" & .Name & "'   Height: " & .Size.Height & "   Weight: " & .Size.Weight
    End With
    
    With oPers2
        MsgBox "'" & .Name & "'   Height: " & .Size.Height & "   Weight: " & .Size.Weight
    End With

End Sub
 
Upvote 0
If you also want to add a handy Parent Property to the Size Class in order to be able to go up the Object tree one level back and retrieve the Person Object then here is an updated version :

1- C_Person Class :
Code:
Option Explicit

Private oSize As C_Size
Private sName As String
Private lCounter As Long

Public Property Get Name() As String
    Name = sName
End Property

Public Property Let Name(ByVal vNewValue As String)
    sName = vNewValue
End Property

Public Property Get Size() As C_Size

    If lCounter = 0 Then
        lCounter = lCounter + 1
        Set oSize = New C_Size
        Set oSize.Parent = Me
    End If
    Set Size = oSize

End Property
2- C_Size Class :
Code:
Option Explicit

Private W As Single
Private h As Single
Private oPr As C_Person

Public Property Get Weight() As Single
    Weight = W
End Property

Public Property Let Weight(ByVal vNewValue As Single)
    W = vNewValue
End Property

Public Property Get Height() As Single
    Height = h
End Property

Public Property Let Height(ByVal vNewValue As Single)
    h = vNewValue
End Property

Public Property Get Parent() As C_Person
    Set Parent = oPr
End Property

Public Property Set Parent(ByVal vNewValue As C_Person)
    Set oPr = vNewValue
End Property
3- Usage code :
Code:
Option Explicit

Sub Test()

    Dim oPers1 As C_Person
    Dim oPers2 As C_Person

    Set oPers1 = New C_Person
    Set oPers2 = New C_Person
    
    With oPers1
        .Name = "Albert"
        .Size.Height = 184
        .Size.Weight = 90
    End With
    
    With oPers2
        .Name = "Claire"
        .Size.Height = 172
        .Size.Weight = 75
    End With
    
    With oPers1
        MsgBox "'" & .Name & "'   Height: " & .Size.Height & _
        "   Weight: " & .Size.Weight
    End With
    
    With oPers2
        MsgBox "'" & .Name & "'   Height: " & .Size.Height & _
        "   Weight: " & .Size.Weight
    End With
    
    MsgBox [COLOR=Red][B]oPers1.Size.Parent.Name[/B][/COLOR]

End Sub
 
Upvote 0
I'm running into an object required error and I can't help but think it has something to do with the inheritance. The following is my attempt at replicating the if statement in the object property.

Public Property Get NodeStart() As Node
If iCount1 = 0 Then
iCount1 = iCount1 + 1
Set cNodeStart = New Node
End If
NodeStart = cNodeStart
End Property
Public Property Let NodeStart(Value As Node)
cNodeStart = Value
End Property

The error shows up as soon as I try to assign values to the Node properties as shown in the following section of code

newMem.Name = "Column"
newMem.NodeStart.x = centerCol

Clearly the routine is assigning values to the newMem object but throws the error on the NodeStart stating that an Object is Required.

I'm missing something here and any help would be greatly appreciated.
 
Upvote 0
I managed to initialize the object apparently because now I'm getting the error as a "Object Variable or With Block Variable Not Set" error in the line - NodeStart = cNodeStart.
 
Upvote 0
I still haven't figured this one out if anyone can take a shot. I'm basically creating a cascade of nested objects.

Thanks for any help
 
Upvote 0
I still haven't figured this one out if anyone can take a shot. I'm basically creating a cascade of nested objects.

Thanks for any help


Hi. Could you explain what you want in general terms ? It would be helpful if you can give a brief example using the C_Person Class I posted above and tell us what you expect.
 
Upvote 0

Forum statistics

Threads
1,224,557
Messages
6,179,507
Members
452,917
Latest member
MrsMSalt

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