Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set property of vba class with object reference

Tags:

vba

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?

like image 440
Iain Sproat Avatar asked Feb 23 '11 15:02

Iain Sproat


People also ask

What is object properties VBA?

A property is an attribute of an object that defines one of the object's characteristics, such as size, color, or screen location, or an aspect of its behavior, such as whether it is enabled or visible. To change the characteristics of an object, you change the values of its properties.

How do I add a property in VBA?

From the View menu of the Visual Basic Editor, choose Properties window (F4). Select the object whose properties you want to display. You can either use the mouse to select the object or use the Project Explorer to choose from a list.


2 Answers

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
like image 43
Aleksey Avatar answered Nov 15 '22 16:11

Aleksey


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
like image 86
mwolfe02 Avatar answered Nov 15 '22 17:11

mwolfe02