aikimark
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.
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?
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
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
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?
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
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
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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:
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
ASKER
Thanks.
You're welcome!
Open in new window
Module:
Open in new window