I have a global and local variable with the same name. If I change the local variable, it overrides the global one. Example below. Demo is also attached.
This is not how it works in other languages. A local variable will never override a global variable.
Bug or "feature"?
Local variable overrides global:
Sub Globals
Private myvar As String = "Global"
Private EditText1 As EditText
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
EditText1.Text = "Local"
End Sub
Sub Button1_Click
Private myvar As String = EditText1.Text ' Shouldn't override global myvar!
ToastMessageShow ("Local variable myvar set to '" & myvar & "'", True)
End Sub
Sub Button2_Click
MsgboxAsync (myvar, "Showing global myvar")
End Sub
Not a bug. There could never be a local variable with the same name as a global variable, this means that myvar is a global variable. Even if you declared it again.
Not a bug. There could never be a local variable with the same name as a global variable, this means that myvar is a global variable. Even if you declared it again.
This is very unexpected and unfortunate. I have yet to see another programming language that won't allow local variables with the same name as globals. (That's one of the main ideas behind local variables after all - to avoid name conflicts)
Could you perhaps make a compiler warning: Warning: Local variable overrides global with the same name.
take a look at main.java in your Objects folder. you'll see how the global myvar is declared and then, further down in the button_click() sub, the java translation essentially ignores the local myvar. it assigns your edittext.text value to the already existant global myvar. it is what it is.
Your question misses the point. The point is, you should never have to wonder if you somewhere have declared a global variable named the same as your local one. Never!
That said, global variables should be avoided if possible (because of issues like this and similar) but you can't always avoid it.
I too would prefer it not to be so (and I think it would be very useful to have also local static variables), but this is the situation, you just have to be careful.
I personally don't use common variable names. Just the classic "i", "j" as cycle counters, but using them like this, they are initialized every time.
For module-level variables, I always use names that make sense and letter "m" as a prefix ("g" for process globals).
If you declare at local variable and a global variable exists with the same name, the local variable well override the global variable. This can lead to hard-to-debug errors. Wish 1: Local variables should not override global variables. This is like it is in virtually all other programming...
run the attached and check the log. it's user larsen's test done in inline java. global is global and global is local and global global keeps his identity even after local global is assigned. Schrödinger's cat is alive and dead at the same time.
languages like for example PHP support this and you can use GLOBAL myvar in a sub when you want to use the global one. otherwise it will create a sub based one.
Not specifically on-topic, but, I "grew up" in a place that had a "shop standard" that required every variable to have a two-character code prefix to indicate the scope and type of the variable to assist in peer code review. For example, if you saw "gsMyVar" in a statement you would know it was a global-scope string variable without having to go find its declaration, and if that variable didn't make sense in that statement it was a clue to look deeper.
Having coded that way for an eternity it's second-nature to me now and it has saved me countless hours of debugging. Your mileage may vary.