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?
LVL 47
aikimarkAsked:
Who is Participating?
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
I guess there is an issue if you use a class variable as an counter in for loop.
If you have to use that variable in loop, this seems to work

Sub testClassPublicVar()
    Dim cThing As New Class1
    Do Until cThing.lngLoop = 5
        cThing.lngLoop = cThing.lngLoop + 1
        Debug.Print cThing.lngLoop
    Loop
End Sub

Open in new window

0
 
Martin LissOlder than dirtCommented:
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

0
 
Martin LissOlder than dirtCommented:
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

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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

0
 
aikimarkAuthor Commented:
@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?
0
 
aikimarkAuthor Commented:
A promising work-around, Neeraj.
1
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Thanks! Glad you found it useful.
0
 
Fabrice LambertFabrice LambertCommented:
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

0
 
aikimarkAuthor Commented:
Thanks.
0
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You're welcome!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.