Global Variables

Post your Gambas programming questions here.
Post Reply
AndyGable
Posts: 362
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Global Variables

Post by AndyGable »

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: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: Global Variables

Post by PJBlack »

' 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: 173
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: Global Variables

Post by Quincunxian »

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: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: Global Variables

Post by PJBlack »

speaking names quin ... and gb has completion hints after three letters
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Global Variables

Post by stevedee »

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: 173
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: Global Variables

Post by Quincunxian »

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: 187
Joined: Wednesday 5th December 2018 5:00am
Location: Marengo, Illinois usa

Re: Global Variables

Post by grayghost4 »

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: 362
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Global Variables

Post by AndyGable »

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