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 48
aikimarkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.