Hi, I'm sure it can be done but I can't seem to find an example or the way to do it. I have a number of variables, PS1, PS2, PS3, PS4, PS5 etc and I want to set them all to '0' at present I do it line by line eg.
PS1 = 0
PS2 = 0
PS3 = 0
PS4 = 0
PS5 = 0
I'm sure there must be a way to set them all to 0 on one line, something like
(PS1, PS2, PS3, PS4, PS5) = 0
Just another stumbling block I'm frustrated with as I know I've seen it done
Cheers for the replies, they are global variables and need to be reset during the game, it would be nice if you could set variable names like you could in good old VB6 as an array.
May be this is something that could be developed in the designer, i.e. when you create a variable in VB6 say (PS1)in the designer and then copy and paste it asks if it's an array, if you said yes the array would be created and the original variable would be PS1(0) and the copied one PS1(1) subsequent copies and pastes would continue the array PS1(2), PS1(3) etc
Therefore in B4A etc to change would simply be a for next loop
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
Dim p(6) As Int 'all are zero
setAllTo(p, 9)
Log(p(5)) '9
End Sub
Private Sub setAllTo(ar() As Int, cnst As Int)
For i = 0 To ar.Length -1
ar(i) = cnst
Next
End Sub
I agree. Maps are almost always the most useful structures (the only exception, in my opinion, are Lists of Custom Types, which have the advantage of sorting).
[This is almost certainly not the case, but a "fascinating" thing, now archaic because we can afford to throw away tons of memory, are operations on bits].
Just to have a single line?
1 - I don't like long lines at all
2 - What if the variable names were longer (which should be done, since PS1 doesn't mean anything, if not "Play Station 1", ? which is now very old)?
Just to have a single line?
1 - I don't like long lines at all
2 - What if the variable names were longer (which should be done, since PS1 doesn't mean anything, if not "Play Station 1", ? which is now very old)?
"fill" is a Java method on Arrays. See example below.
(This is two lines. The call to "setAllTo" in post #9 is only one line, and is B4X syntax, instead of Java.)
B4X:
Dim p(6) As Int 'all are zero
Dim Arrays As JavaObject
Arrays.InitializeStatic("java.util.Arrays").RunMethod("fill",Array(p,999))
Log(p(3)) '999
its just the last two parameters that are important ps and 9
ps is the array name you want to set to known values
9 is the value you want each ps(0) ... ps(n) to hold.