Iain Sproat
Iain Sproat

Reputation: 5330

Set property of vba class with object reference

I have a class module, named Normal, in VBA with the following code:

Private mLine As LineElement

Public Property Get Line() As LineElement
    Line = mLine
End Property

Public Property Set Line(ByRef vLine As LineElement)
    mLine = vLine
End Property

This class is used by the following code:

Sub Run
    Dim Line As LineElement
    Set Line = New LineElement

    Dim Norm As Normal
    Set Norm = New Normal
    Set Norm.Line = Line 'FAILS here with "Object Variable or With Block Variable not set"'
End Sub

Also, if I change the code in the Normal class module to:

Private mLine As LineElement

Public Property Get Line() As LineElement
    Line = mLine
End Property

Public Sub SetLine(ByRef vLine As LineElement) 'changed from property to sub'
    mLine = vLine
End Property

and the failing line to

Norm.SetLine( Line )

I get an "Object does not support this property or method" error. What exactly am I doing wrong in both of these cases?

Upvotes: 17

Views: 54547

Answers (4)

Hamidreza.Kiani
Hamidreza.Kiani

Reputation: 57

Try this

Private mLine As new LineElement

Public Property Get Line() As LineElement
    Set Line = mLine
End Property

Public Property Set Line(ByRef vLine As LineElement)
    Set mLine = vLine
End Property

Upvotes: 1

user7336106
user7336106

Reputation:

The Set statement is used to make a reference of an object to an object variable. You don't have to use the Set keyword, if you are dealing with primitive and native built-in types such as integer, double, string and so on. Here you are dealing with an object of type LineElement.

Upvotes: 0

mwolfe02
mwolfe02

Reputation: 24207

Try this:

Private mLine As LineElement

Public Property Get Line() As LineElement
    Set Line = mLine
End Property

Public Property Set Line(ByRef vLine As LineElement)
    Set mLine = vLine   'Note the added Set keyword in this line'
End Property

Upvotes: 22

Aleksey
Aleksey

Reputation: 487

Both properties must have "Set" keyword

Private mLine As LineElement

Public Property Get Line() As LineElement
    Set Line = mLine 'Set keyword must be present
End Property

Public Property Set Line(vLine As LineElement) ' ByRef is the default in VBA
    Set mLine = vLine 'Set keyword must be present
End Property

Upvotes: 6

Related Questions