Global Variables

Post your Gambas programming questions here.
Post Reply
AndyGable
Posts: 48
Joined: Wednesday 02nd December 2020 12:11am
Location: England
Contact:

Global Variables

Post by AndyGable » Wednesday 30th December 2020 12:21am

Hi All,

I was wondering if someone could direct me to the correct outcome of this issue I am having

I want to have a module that holds all my global settings for example

Code: Select all

Public Key_SignOn As Integer = 65             'A
the idea is so I can use the same declare though out my application like this

Code: Select all

Select Case Key.Code
    Case Key_SignOn ' lower case A
      frmSignedOff.Enabled = False
      FMain.Workspace1.Add(FrmSignOn, False)
  End Select
and not like this

Code: Select all

elect Case Key.Code
    Case 65 ' Lower case A
      frmSignedOff.Enabled = False
      FMain.Workspace1.Add(FrmSignOn, False)
  End Select
but when I try to run the program I get the following error "Unknown identifier: Key_SignOn in frmSignedoff.class:13"

what do I need to do to get this to work.

I do come from a Visual Basic 6 background and I am willing to learn new skills here

User avatar
PJBlack
Posts: 99
Joined: Tuesday 09th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: Global Variables

Post by PJBlack » Wednesday 30th December 2020 12:55am

' Modul AllMyGlobalVars
Public Key_SignOn As Integer = 65
' Class/Form/Modul blablahblubb
Select Case Key.Code
    Case AllMyGlobalVars.Key_SignOn ' lower case A
BUT

maybe you like to have a look at the gb.settings class ...

http://gambaswiki.org/wiki/comp/gb.settings?ht=settings
or
https://gambas-buch.de/doku.php?id=k19:k19.1:start (german, not translated yet)

User avatar
Quincunxian
Posts: 112
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: Global Variables

Post by Quincunxian » Wednesday 30th December 2020 5:24am

Do yourself a favour and don't name your modules too long - it gets very stale, very quickly, having to type MyFunkySpecialModule.{my variable}

I went full 1980's and named my two main modules:
AV { application variables } - just global variables
AG { application global } - Global functions and routines

...as long as you don't go overboard with shortened names, it will save you a LOT of time as you code.
Cheers - Quin.
I code therefore I am

User avatar
PJBlack
Posts: 99
Joined: Tuesday 09th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: Global Variables

Post by PJBlack » Wednesday 30th December 2020 7:14am

speaking names quin ... and gb has completion hints after three letters

User avatar
stevedee
Posts: 376
Joined: Monday 20th March 2017 6:06pm

Re: Global Variables

Post by stevedee » Wednesday 30th December 2020 8:42am

Quincunxian wrote:
Wednesday 30th December 2020 5:24am
Do yourself a favour and don't name your modules too long...
Quin, I agree with PJ and rely on auto-complete to do most of my typing. But I try to avoid similar long names:-

mySpecialFunkyChicken
mySpecialFunkyChildren

...but even in such cases, I just use the up/down arrow when auto-complete gives me a list of options.
I'd probably call the modules: gVars & gFunctions

I don't want to go back to the 1980s for x = y - z + 2j

...I'd only go back for ABBA

User avatar
Quincunxian
Posts: 112
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: Global Variables

Post by Quincunxian » Wednesday 30th December 2020 11:42pm

There is one exception.
If you assign a local variable to a global variable in a form or a class then the auto-complete does not work.
' Gambas class file
Private MyLocalVariable as string = MyGlobalModule.SomeVariable  ' Auto-complete does not work

Public Sub Form_Open()
   Dim MyLocalVariable as string = MyGlobalModule.SomeVariable  ' Auto-complete works
End
As a whole, I agree to a point.
Normally I only use three distinct modules and avoid using more as they do come with a memory overhead.
So having short names for these especially with the naming convention used ( AE, AG, AV ) does save time ( AE is dedicated to error management routines )
I make extensive use of tool-box classes that I've developed over the years and the contained routines do need to be named distinctively for obvious reasons.

My advice to new coders is: be consistent.
If you work in a professional developer team, they will normally have a style guide on what naming convention to use.
Even if you are just an enthusiast, then being consistent makes life easier in the long run.
I've opened code I wrote 10 years ago and with a horrified look on my face, I had that moment of "What the hell was i thinking..."
Cheers - Quin.
I code therefore I am

User avatar
grayghost4
Posts: 83
Joined: Wednesday 05th December 2018 5:00am
Location: Concord, CA usa

Re: Global Variables

Post by grayghost4 » Wednesday 30th December 2020 11:56pm

I've opened code I wrote 10 years ago and with a horrified look on my face, I had that moment of "What the hell was i thinking..."
I have had that happen with what I wrote last week :lol:

AndyGable
Posts: 48
Joined: Wednesday 02nd December 2020 12:11am
Location: England
Contact:

Re: Global Variables

Post by AndyGable » Sunday 03rd January 2021 3:50pm

Thank-you to everyone who replay Sorry for the dalay I only just got my emails back up and running

Post Reply