Calling Subs/Funct in other Modules

Post your Gambas programming questions here.
Post Reply
nmw64
Posts: 3
Joined: Saturday 31st October 2020 12:41pm

Calling Subs/Funct in other Modules

Post by nmw64 »

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??
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Calling Subs/Funct in other Modules

Post by cogier »

Try changing the top lines to: -
' Gambas module file
Public DBCon As New Connection

Public Sub Main()
.......
nmw64
Posts: 3
Joined: Saturday 31st October 2020 12:41pm

Re: Calling Subs/Funct in other Modules

Post by nmw64 »

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
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Calling Subs/Funct in other Modules

Post by cogier »

OK here's the 2nd attempt. Try this: -
If DataBase_Module.Database_Connect(DBCon) = True Then
  ' database connected
Else
    ' Database failure
Endif
nmw64
Posts: 3
Joined: Saturday 31st October 2020 12:41pm

Re: Calling Subs/Funct in other Modules

Post by nmw64 »

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!!
User avatar
Quincunxian
Posts: 171
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: Calling Subs/Funct in other Modules

Post by Quincunxian »

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.
Cheers - Quin.
I code therefore I am
Post Reply