I am a new gambas user and testing it out for a future project.
I created 2 module files to split my code, Startup and Database.
In the startup i have defined a Sub Main and set it to be the starting point for the application
' Gambas module file
Public Sub Main()
Dim DBCon As New Connection
If (Database_Connect(DBCon) = True) Then
' database connected
Else
' Database failure
Endif
FMain.Show
End
In the Database Module i have the Public Function Database_Connect
Gambas module file
Public Function Database_Connect(DB As Connection) As Boolean
DB.Name = "mysql"
DB.Host = "localhost"
DB.Login = "root"
DB.Password = "password"
Try DB.Close()
DB.Open()
Return True
End
When running the application i get the error Unknown Identifier Database_Connect in Startup.module:7
So Database connect is not seen by the program as anything, but it is a function declared as Public
Can anybody help me out on this one??
Calling Subs/Funct in other Modules
- cogier
- Site Admin
- Posts: 1127
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Calling Subs/Funct in other Modules
Try changing the top lines to: -
' Gambas module file
Public DBCon As New Connection
Public Sub Main()
.......
Re: Calling Subs/Funct in other Modules
No different,
It is the entire routine Database_Connect that has the issue, it is just not recognised in module start-up as a function in module database.
Its like the function database_connect is declared as private!!!
Do i need to add some sort of reference in the module startup to get a list of the public functions in the module database.?
Nmw64
It is the entire routine Database_Connect that has the issue, it is just not recognised in module start-up as a function in module database.
Its like the function database_connect is declared as private!!!
Do i need to add some sort of reference in the module startup to get a list of the public functions in the module database.?
Nmw64
- cogier
- Site Admin
- Posts: 1127
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Calling Subs/Funct in other Modules
OK here's the 2nd attempt. Try this: -
If DataBase_Module.Database_Connect(DBCon) = True Then
' database connected
Else
' Database failure
Endif
Re: Calling Subs/Funct in other Modules
Okay got it.
Firstly do not call a module Database, it get very confused with an internal type!!
Rename Database module as DBRoutines module
Then in code use an implicit call
If DBRoutines.Database_Connect(DBCon) = True Then
' database connected
Else
' Database failure
Endif
Thanks for the help, it go me looking in the right direction!!
Firstly do not call a module Database, it get very confused with an internal type!!
Rename Database module as DBRoutines module
Then in code use an implicit call
If DBRoutines.Database_Connect(DBCon) = True Then
' database connected
Else
' Database failure
Endif
Thanks for the help, it go me looking in the right direction!!
- Quincunxian
- Posts: 173
- Joined: Sunday 25th June 2017 12:14am
- Location: Western Australia
Re: Calling Subs/Funct in other Modules
Two lessons I leant about modules.
1. Modules load alphabetically so be careful of dependencies if you are cross referencing variables or routines between them.
2. Keep Module names as short as practicable. My projects tend to have three modules. AE, AG and AV
AE = Application Error Management - which has all the application error management routines.
AG = Application Global - Which has all global routines common to most of my applications. (Initialisation, Display sub forms, load picture ect...)
AV Application Variables - This is specific to the program where global variables are required.
Why ? I just got so sick and tired of typing long names for modules that I use frequently. It improved my programming speed to a noticeable degree.
(...and this specific naming convention also grouped my modules together at the top in the GUI)
Using modules for database management.
There may be times when you want to instantiate multiple connections to your database. If your connection is through a Module.Sub_main() then you can only do this once.
It may be better to do this through a toolbox class, so that you can instantiate as many instances as you require.
A toolbox class is simply a name for a class that you can copy & paste from one project to another to perform a specific set of functions that has little or no dependencies.
1. Modules load alphabetically so be careful of dependencies if you are cross referencing variables or routines between them.
2. Keep Module names as short as practicable. My projects tend to have three modules. AE, AG and AV
AE = Application Error Management - which has all the application error management routines.
AG = Application Global - Which has all global routines common to most of my applications. (Initialisation, Display sub forms, load picture ect...)
AV Application Variables - This is specific to the program where global variables are required.
Why ? I just got so sick and tired of typing long names for modules that I use frequently. It improved my programming speed to a noticeable degree.
(...and this specific naming convention also grouped my modules together at the top in the GUI)
Using modules for database management.
There may be times when you want to instantiate multiple connections to your database. If your connection is through a Module.Sub_main() then you can only do this once.
It may be better to do this through a toolbox class, so that you can instantiate as many instances as you require.
A toolbox class is simply a name for a class that you can copy & paste from one project to another to perform a specific set of functions that has little or no dependencies.
Cheers - Quin.
I code therefore I am
I code therefore I am