# Probably a silly newbie question but

I can declare a list of local variables and initialise them all to zero.
eg
Var a,b,c,d,e,f,g,h as integer = 0

My routine loops through a dataset and changes these variables which I then use accordingly.
If I now move to the next record and I want to reset these variables to zero. I could use some sort of method I suppose to do this, but to keep the code really simple is there a way to reset them as a one line list again or do I have to (as I am doing) reset each to zero on separate lines
ie
a = 0
b = 0 etc

thanks

As far as I know you’re only initialising h to zero.

And yes you have to reset them all to zero one at a time.

One way to do it is to initialise them inside the loop

current:

``````Var a,b,c,d,e,f,g,h as integer // you don't need to add = 0 here, for a simple variable type
for x as integer = 0 to something
//get stuff
//do stuff
a=thing
b = thing2
next
``````

might become

``````
for x as integer = 0 to something
Var a,b,c,d,e,f,g,h as integer
//get stuff
//do stuff
a=thing
b = thing2
next
``````

But if ALL of these variables are set to something specifc inside the loop, every single time, then you dont even need to reset them back to 0

``````Var a,b,c,d,e,f,g,h as integer
for x as integer = 0 to something

//get stuff
//do stuff
a= thing   //if a is always set to thing, then setting it to 0 is a waste of time
b = thing2
next
``````
2 Likes

This is the first time I read this, for me (I think):

``````Var a,b,c,d,e,f,g,h as integer = 0
``````

is the same as

``````Var a,b,c,d,e,f,g,h as integer
``````

all set to 0.

If we use

``````Var a,b,c,d,e,f,g,h as integer = 1
``````

then all are set to 1.

1 Like

Yes, that appears to be the case, although the Docs at Var → Usage is unclear. There is a note in Var → Notes, however.

1 Like

maybe process the row in a own method

pseudocode

``````for each row in rowset
process(row)
next

function process(row)
Var a,b,c,d,e,f,g,h as integer
do something with the row
``````

Why not reset them to 0 as soon as your are done ‘using them accordingly’.

``````Var nums(8) As Interger
Var results() As Integer

For Each n as integer in nums
n=1+n^2/(n+1)  //or whatever
n=0
Next
``````

Or you could call this after you are done with your variables

``````For Each n as integer in nums
n=0
Next
``````

To illustrate the first options more completely, create a new project. In the window, drag a button. Add the following code to the button’s Pressed event handler:

``````var nums(8) As integer
var anothernum As integer=rnd*100005
var results() As string
var numsString() As string

For each n As Integer In nums
n=(10+anothernum)/anothernum^2
anothernum=rnd*anothernum
n=0
Next

MessageBox("Results Array: " + chr(13) + string.FromArray(results,", ") + chr(13) +_
"Original Array set back to 0: " + chr(13) + string.FromArray(numsString, ", "))
``````

This is a little wordy because I wanted the results in a message box.

Regards,

Tom

Thanks for replies - answer is “No” then…

Thanks for workarounds - I perhaps should have said these variables are counting stuff ( a=a+1) so have to re -initialise outside the loop.
Passing the row to a method would work, but I guess I then have to think about scope or forming them into an array and calling a method to initialise that would save some lines - there are quite a few of them!

Regards

Larry

if you initialise inside the loop, as shown above, then they do all get set to zero at once.

An alternative would be to use a memoryblock formed of (say) 8 int32 records.
Instead of variable A, you could use

` thememoryblock.uint32(2) = thememoryblock.uint32(2) +1`

When you want to reset, set the whole memoryblock to 0 by using

`thememoryblock.stringvalue = ""`

Or, use an array, and do it in 2 lines of code
eg

``````
Thearray.resizeto(-1)
Thearray.resizeto(10)
``````

Having said all this, this discussion has taken a lot longer than the few extra lines of code you were hoping to save. I doubt it’s worth it in terms of either amount of code, or speed of execution.

Hi Jeff
The way I have written this routine does not lend itself to that solution. - I did it to make it easier to follow, but in essence I have 15 variables which do the counting on different columns in a returned row ( it is a statistics exercise ). Had I written in a more condensed way, your solution would, of course, be fine - but it would been have harder to follow for an amateur like me.
You are also right that doing it the long way of re-initalising on separate lines works perfectly well, adds no significant overheard through a thousand or so records and even at the time I thought I should build some data structure to do it more elegantly.
My query was really a language learning one - I was just puzzled that I could initialise on one line at declaration, but not again later.

Regards Larry

If you create a class (eg MyVars)
which has public properties A,B,C,D etc

Then before your loop, you do this:

`Var P as new MyVars`

All the properties are now 0

``````
P.A = P.A+1
P.C = P.C *3
//etc
``````

To set all of the values to 0 in one line, just do this:

``````
P = new MyVars
``````

I say ‘set them all to 0 in one go’, but because it’s a class, you could set indvidual properties to specific numbers in the constructor.

Thanks Jeff - now even I can see that is a very elegant fix to a scrappy chunk of code!

What I do for this case when there are many variables to reset is to put them into a record with a constructor to clear all of them. That way you can reset all of them at once. Plus if a new variable is added to the group it is automatically reset as well.

Then just define them in the loop.

2 Likes