Same Var Name Local Global

I had a code in a button managing a local variable. I modified my program and create this variable as global (in the window property), but I forgot to remove this variable from my Dim in my button. And I didn’t have any alert when analyse my project.

Not sure to be clear, here is a sample code. Search for MyVarInt to understand.
Launch the project and click the button. The MessageBox display " MyVarInt = 1" each time you click it.
Remove the line “Dim MyVarInt as Integer” in Button1.Press , relaunch and click the button. The MessageBox display " MyVarInt = 11" then 12, 13 and so on.

I am nearly sure that in the past, Xojo displayed me a warning like : “A variable is used twice and it’s not sure which one to refer” or something like that.
When we have many variables, it’s not so easy to see the problem.

1 Like

The easiest way to avoid this is by how you name your variables / properties / parameters:

For example,
• prefix property names with m (for member)
(we also add an underscore suffix to private property names - eg: mName_)

• prefix parameter names with p

• prefix public module variables with g (for global)

So… changing a variable from a local variable to a property would mean renaming it from ‘name’ to ‘mName’ / ‘mName_’ and updating any relevant code.

Apart from helping you to avoid name conflicts it also helps to make code more readable.

Create a Module, call it for example “Globals”, inside it create your global properties, like:

Protected Property IsLoaded As Boolean = False

Then use it in the app calling its full name Globals.IsLoaded, and you can have another local IsLoaded without problems.

Avoid Globals, but if you need to, concentrate them isolated (Protected) in a easy to find “Namespaced” place.

It does not, and did not. What you did accidentally I often do purposefully, usually for debugging purposes.

You might be thinking of the case when a subclass has defined the same property name as its superclass. That’s shadowing, will generate a warning, and usually should be avoided.

Thank you for yours answers. I effectively read that variable should be named differently depending of Local/global etc. but I didn’t do that when I began and it would be too long to change in all my projects.

Then I was wrong, Xojo never warn about this kind of mistake.
And reading you, it shouldn’t be a feature request? In Rick exemple, we use our global variable as MyGlobals.MyVariable . Then if we use only MyVariable and have it in Global, in a window and in a Method (local), Xojo would be able to warm us.

Then my 2th question is : May I ask a feature request for this ?

You mean, if by mistake you forgot to make it protected and its scope is global, in such way you don’t need it namespaced, and you, by any chance, created a local one overriding it?

Well, it should be hard to be done, you should commit 2 different errors at the same time to commit such mistake, rare case, but if the compiler could easily detect such clash and emit a warning if you opt-in in doing so, ok.

But I don’t think you will see it implemented… soon…

Better take actions to fix your code right now with the tools you have.

I no longer change my property names as it makes it a pain to change their scope later. As convention, I initial lowercase local variable names in code, and initial uppercase properties.

But whatever works for you.

You can certainly request this, or any other, change, but I doubt it will happen as it’s been this way for far too long, and is usually not an issue.

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.