I have a class called Vars that consists of a group of re-usable variables used throughout my project. I used a class for this to make the variables visible everywhere. kind of like the ‘Type’ construct used in VB. One of the class variables is an integer used for a general purpose counter. It is invoked by typing ‘v.counter’ after the
Dim v as new Vars. If I try to use v.counter in a for next loop, I am getting a syntax error when I try to run the routine.
The error is happening on this line of code;
For v.counter=1 to 11 . Just to be certain I do have a corresponding Next further down.
The LR for “For … Next” loop mentions the allowed types of “counter” as Integer, Single or Double, while the example above uses class “Vars” variable counter. This may be causing syntax error.
I am by no means an OOP guru, but IMHO, what you are trying to do runs counter to the principles of OOP. By trying to expose the properties of your class to a global scope, you are countering the principle of encapsulation. Either 1) let the class handle the incrementing/decrementing of your variables by calling a method of the class, or, call the variable what they are, “global” and expose them either as members of the app object or in a global module. Many programmers like to name global variables starting with a small g (i. e. gMyCounter)
I suppose I could just have declared a counter locally within the routine and saved myself a lot of bother, but I was more interested in what rules I could be breaking and what the limitations of classes were in relation to the fundamentals of programming. For me a declared integer is a member of a class the same way that a custom class is.
Certainly you can declare an integer as a member of a class. But the class should handle that integer and it should not them be exposed globally. When you declared counter as a member of the class vars, did you set its score to public?
[quote=121112:@Roger Clary]Certainly you can declare an integer as a member of a class. But the class should handle that integer and it should not them be exposed globally. When you declared counter as a member of the class vars, did you set its score to public?
[/quote]
So essentially this is a scope issue (it was set globally)
At this time it can only be guessed that Xojo may be expecting a single identifier variable (without the “.”) as the “counter” for a “For … Next” loop and that could be the reason for “syntax error”. The same v.counter can be assigned any Interger value within the allowed range and Xojo does not show any syntax error.
[code]Dim intVar As Int32
Dim v As New Vars
v.counter = 1 // No syntax error
intVar = 2
v.counter = intVar // No syntax error
For intVar = 1 To 11 // No syntax error
v.counter = intVar
Next // No syntax error
For v.counter = 1 To 11 // Syntax error
intVar = v.counter
Next // Syntax error[/code]
Also, for what its worth, youre not “reusing” anything if you say
dim v as new vars
Youre just creating a new instance of the object which contains the variables. Which in turn means that youre probably using more memory than you would otherwise.
The “counter” property is of type “Integer” in class “Vars” and the “For … Next” loop, according to LR, should accept this as the “counter” instead of showing a syntax error. May be the LR needs to be updated.
The word “variable” used in LR for the “For … Next” loop “counter” definition/description requires better qualification/definition.
A property of type Integer in the main Window1 (form) in a Desktop application also results in similar syntax error as shown below in Xojo 2014r2 on Window 7.
[code] //
// Window1 Form Open event.
//
// intVar is of type Integer property of Window1
//
// None of the next four lines of code show syntax error.
intVar = 1
For intVar = 1 To 11
intVar = intVar
Next
Window1.intVar = 1 // No syntax error
For Window1.intVar = 1 To 11 // Syntax error
intVar = intVar
Next // Syntax error
[/code]
Module Vars
Global Property Counter As Integer
...
End Module
Then you can use it elsewhere like this - of course only if there is no variable with the name “Counter” shadowing you global Counter variable:
For Counter = 0 To 10
...
Next
But I would recommend to look for another solution - application-architecure-wise this is bad design. One should avoid global variables.
[quote=121118:@Greg O’Lone]Are you aware that you can declare a variable in the for-next loop?
For i as Integer = 1 to 10
// do something
Next i
[/quote]
No, I had no clue you could do that. I knew that you could dimension and assign a value on one line, but didnt know that it could be done in a For, Next loop. Gracias Amigo.