Link to home
Start Free TrialLog in
Avatar of aikimark
aikimarkFlag for United States of America

asked on

Getting "Variable required - can't assign to this expression" error

I'm receiving the following compile error when using a public class variable in a looping/iteration statement in a VBA environment:
Compile error:

Variable required - can't assign to this expression


For simplified illustration purposes, create the following class in an empty VBproject in some Office product.
Option Explicit

Public lngLoop As Long

Open in new window

Next, insert a module and add the following routine:
Sub testClassPublicVar()
    Dim cThing As New Class1
    For cThing.lngLoop = 1 To 5
        Debug.Print cThing.lngLoop
    Next
End Sub

Open in new window

If you compile or run the sub, you should see the error message.

I have assigned and retrieved values from such public class variables without error.  What's going on here and is there any way around this?
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Class:
Option Explicit

Private lngLoop As Long
Public Property Get Loop_Value() As Long
    Loop_Value = lngLoop
End Property
Public Property Let Loop_Value(Value As Long)
    lngLoop = Value
End Property

Open in new window


Module:
Sub testClassPublicVar()
    Dim cThing As New Class1
    Dim lngL As Long
    
    For lngL = 1 To 5
        cThing.Loop_Value = lngL
        Debug.Print cThing.Loop_Value
    Next
End Sub

Open in new window

And if lngLoop were Public (it really shouldn't be) you can just do this.

Sub testClassPublicVar()
    Dim cThing As New Class1
    Dim lngL As Long
    
    For lngL = 1 To 5
        cThing.lngLoop = lngL
        Debug.Print cThing.lngLoop
    Next

End Sub

Open in new window

Martin is right. If lngLoop is declared as a Public variable in the class module, you can use this like below...

Dim cThing As New Class1
Dim i As Long
For i = 1 To 5
    cThing.lngLoop = i
    Debug.Print cThing.lngLoop
Next

Open in new window

Avatar of aikimark

ASKER

@Martin

The reason I defined a public variable in the class was to provide some variables just by instantiating the class.  I didn't want the class consumer to have to create local variables to be used with each class instance.

=========================
@Neeraj

I know that's possible.  Why can't I use the public class variable in the For statement?
ASKER CERTIFIED SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A promising work-around, Neeraj.
Thanks! Glad you found it useful.
If I can put my grain of sand here,  class shouldn't be used as data containers but as services providers.
If all you want is aggregate data,  there are user defined types available for this purpose.

Try the following:
Type myType
    loopVar As Long
End Type

Public Sub testSub()
    Dim thing As myType
    For thing.loopVar = 1 To 5
        Debug.Print thing.loopVar
    Next
End Sub

Open in new window

Thanks.
You're welcome!