Hi.
As I use a lot of For / Next loops in my program, I thought of using the same variables in each Sub, so I wanted to declare them as Public like I was used to in RealBasic. However, that seems not possible in Gambas. If I put f.e. :
' Gambas class file
Public x As Integer
-----------------------------------
Public Sub Form_Open()
For x = 1 To 10
‘Some code
Next
End
----------------------------------
I get : “Loop variable cannot be global .....”
Does this mean I have to keep declaring such variables in each Sub ?
Greetings
Public Variable Declaration
-
- Posts: 84
- Joined: Wednesday 22nd August 2018 7:55am
Public Variable Declaration
Old african saying:
You eat an elephant one small bite at a time.
You eat an elephant one small bite at a time.
Re: Public Variable Declaration
Yes, but you can also use the following form (In more recent versions of Gambas):
I did not realize you can't use a class level variable for an iterator, so I just learned something new.
Side note: The meaning of "Global Variable" is somewhat nebulous. In Gambas, since there are no "True Global Variables", the term seems to be used for what is more commonly called "Class Level", "Module Level", or even "File Level" variables.
You should be aware of is that the limits of For/Next loops are set before the first iteration and don't change after that. In other words, if you have an expression for the "To" or the "Step" value, it only gets evaluated once. To me, this is a good thing as it saves you from having to create a local variable for efficiency purposes.
Ced
For x As Integer = 1 To 10
Print x
Next
Though my preference is to Dim them separately.I did not realize you can't use a class level variable for an iterator, so I just learned something new.
Side note: The meaning of "Global Variable" is somewhat nebulous. In Gambas, since there are no "True Global Variables", the term seems to be used for what is more commonly called "Class Level", "Module Level", or even "File Level" variables.
You should be aware of is that the limits of For/Next loops are set before the first iteration and don't change after that. In other words, if you have an expression for the "To" or the "Step" value, it only gets evaluated once. To me, this is a good thing as it saves you from having to create a local variable for efficiency purposes.
Ced
.... and carry a big stick!
Re: Public Variable Declaration
I didn't know that either, but there are potential dangers in trying to do this. If your code contains "Wait" instructions you could end up with a new loop starting before the first had finished, which would reset x.Doctor Watson wrote: ↑Friday 5th April 2019 9:43am ...I get : “Loop variable cannot be global .....”
You can of course use code snippets if you have enabled these via Tools > Preferences > Code Snippets.
For example, create a new code snippet with the trigger string: dx
...and the code: Dim intX as Integer
Then in your code, you just type: dx <TAB>
- cogier
- Site Admin
- Posts: 1126
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Public Variable Declaration
Following on from Stevedee's comments you can use this now. Just type: -
di [Tab]
You will get this ready for you to type a variable name
di [Tab]
You will get this ready for you to type a variable name
Dim iVar As Integer
-
- Posts: 84
- Joined: Wednesday 22nd August 2018 7:55am
Re: Public Variable Declaration
Sorry for late reply. I was out of the country for afew days.
Thanks all, I'll see what I can do with it.
Thanks all, I'll see what I can do with it.
Old african saying:
You eat an elephant one small bite at a time.
You eat an elephant one small bite at a time.