Eric
Eric

Reputation: 708

VBA using a Class as a property of another Class

I am having trouble using a Class that I created as a property in another Class. I have two classes, one called Vertex and the other called Edge. Two of the properties of Edge, Parent and Child, are Vertex objects. Here is a method where I try to use them.

Option Explicit

Public Sub OrgChart()

Dim Vertices As Collection
Dim Edges As Collection
Dim vParent As Vertex
Dim vChild As Vertex
Dim eEdge As Edge
Dim rEdgeRow As Range

Set rEdgeRow = ActiveSheet.Range("A1:C1")

Do While Len(rEdgeRow(1, 1).Value) > 0

Set vChild = New Vertex
Set vParent = New Vertex
Set eEdge = New Edge

vChild.Name = rEdgeRow(1, 1).Value
vChild.Dummy = False

vParent.Name = rEdgeRow(2, 1).Value
vParent.Dummy = False

eEdge.Parent = vParent
eEdge.Child = vChild
eEdge.Percent = rEdgeRow(3, 1).Value

Set rEdgeRow = rEdgeRow.Offset(1, 0)

Loop

End Sub

When I run this, I get the error,

"Run-time error '91': Object variable or With block variable not set"

The debugger indicates that it is breaking at the line

eEdge.Parent = vParent

I assume that the issue has to do with the eEdge.Parent not being initialized, but I tried using

Set eEdge.Parent = New Vertex

and I got the same issue.

If it will help, I can post the class code as well, though they are fairly simple, only containing Property Get and Property Let functions.

I apologize if I did anything wrong when posting this; this is my first time posting anything on SO.

Thanks, Eric

Upvotes: 3

Views: 2837

Answers (1)

John Bustos
John Bustos

Reputation: 19544

I might not be understanding this right, but did you try:

Set eEdge.Parent = vParent
Set eEdge.Child = vChild

If they're objects, you need to use a "Set"

... Also, if this doesn't fix it, can you post the code for your Vertex class - You may need to dim the Parent and Child objects differently in its constructor...

Upvotes: 2

Related Questions