|
Post by Roger Cabo on Aug 24, 2023 18:04:06 GMT 1
Hi everyone Today, I discovered that it's possible to use the same variable name to represent with the same content in the GB32 programming environment. I used to think that a variable without a prefix would be treated the same as one with a prefix, whether it was declared as global or local. In the first example: Global a$="12345" a="67890" Msgbox a$ & " " & a a$ is declared as a global variable and initialized with "12345", while `a` is then initialized with "67890". The message box will display "67890 67890." In the second example: Global b$ = "12345" Global b As String b = "67890" MsgBox b$ & " " & b both b$ and b are declared as global variables, and they having same string types. `b$` string is initialized with "12345" and `b` is a string initialized with "67890". The message box will display both values. "12345 67890"
Today I was irritated because I got different results and didn't find a notice that some variables are declared with in the same context. Global b$ = "" Global b As String Normally I use the type prefix in variable name all the time and for every kind of type. But without the type prefixes the code is better to read. If you declare variables you should stay at Global b$, or Global b as String. But anyway..
|
|
|
Post by scalion on Aug 24, 2023 20:52:16 GMT 1
Wow, that's a real problem! I hardly use suffixes anymore, I prefer to indicate AS type. I think it's better to opt to always use the syntax with AS type instead of a suffix. Suffixes were the only option in older versions of GFA-Basic, this possibility has been kept but I think it's not a good thing.
|
|
|
Post by Roger Cabo on Aug 24, 2023 21:02:26 GMT 1
Yes, C++/C# certainly doesn't require suffixes (not prefixes), but if you're unsure, simply hover the mouse over a variable, and voila, you can see it in Rider and Visual Studio. In most cases, while coding, you know what you're using Oui, C++/C# ne nécessite certainement pas de suffixes (pas de préfixes), mais si vous n'êtes pas sûr, passez simplement la souris sur une variable et voilà, vous pouvez la voir dans Rider et Visual Studio. Dans la plupart des cas, pendant la programmation, vous savez ce que vous utilisez
|
|
|
Post by scalion on Aug 25, 2023 11:22:55 GMT 1
En même temps on pourrait imaginer vouloir utiliser 2 variables de même type et ayant le même nom avec des contenus différents, peut-on faire cela en C++ ?
|
|
|
Post by (X) on Aug 25, 2023 13:43:13 GMT 1
There are quite a few situations that can arise in GB32 that lets the coder use the same variable name.
Wow! I never stopped to check how many combinations were possible! Testing might reveal a logical precedence. So far, GFA does not crash, but, the coder may not realise what is actually happening.
Most confusing is: "The suffix can be dropped after declaration".
For example: Dim a% // Can immediately be used as: a = 123 If this were not allowed, this would clear up a lot of concerns. To be fair,the GFA editor prompts the coder to pick which variable name to use, but, you can always over-ride and drop the suffix.
Here are a few examples that I was curious to check out:
While trying to create a second variable of same name I got a prompt to choose which type to use:
I also got this warning, when trying to Trace the value:
If there is a conflict, this produces an error...
Dim a$ = "Test" Dim a! = 3.14 Trace a If there is a variant, it seems to take precedence... This code prints: 123.
Dim a$ = "Test" // This, by default ( no explicit type), is coded as a Variant type. Dim a = 123 Dim a! = 3.14 Trace a
A Local declaration takes precedence over a Global of same name:
Dim a$ = "Test" Global a = 123 Trace a p(321)
Proc p(b) // A Local declaration takes precedence over a Global of same name: Local a = 321 Trace a // Outputs 321 EndProc
In p() procedure; Local is used before Global even if redefined in the procedure, the new/latest value is used outside the procedure:
Dim a$ = "Test" 'Dim a% = 456 // This, by default ( no explicit type), is coded as a Variant type. Global a = 123 Dim a! = 3.14 Trace a p(321) Trace a Do : Sleep : Until Me Is Nothing
Proc p(b) Local a = 654 // If already passed, error: Redefined. Global a = 789 Trace a EndProc
|
|