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
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
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
results.Add(n)
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
results.add(anothernum.ToString)
n=0
numsString.add(n.ToString)
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.
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!
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
Sorry did not mean to dismiss your helpful suggestion.
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.
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.