Passing a variable to a module ?

Post your Gambas programming questions here.
Post Reply
Doctor Watson
Posts: 84
Joined: Wednesday 22nd August 2018 7:55am

Passing a variable to a module ?

Post by Doctor Watson »

Hi.
Just so you know : I have been back to square 1 with everything on my computer. A very huge power surge played havoc on numerous things electric in our neighbourhood, including my computer, its HD and external backup HD that happened just then to be connected. And yes, in spite of an apparently useless surge protector. Had to spend every free second to recover what still could be recovered. Back to business.

Question : How can I pass a variable to a module?

Some of you will remember my post on how to store data within an application (programme?) and building on a suggestion from Bruce, I put my data in a module like this :
' Module DataCloset
x As Integer
NamesStr$ As String

Public Sub Data() As String
' Receive a value for x from Button1_Click() ???
 x = 2   'just an example 
 
Select Case x
Case 1
  NamesStr$ = "John,Michael,Harry,Liam,William,Oliver,James,Peter"
Case 2
  NamesStr$ = "Isabelle,Yvonne,Mary,Patricia,Olivia,Emma,Eve,Amalia,Petra,Fleur"
Case 3
  NamesStr$ = "Fido,Tiger,Bella,Ginger,Momo,Chibi"
End Select

Return NamesStr$
  
End
In Fmain I have a Button that should retrieve the list I want (just for now, later to be replaced by a proper choose routine.)
Public Sub Button1_Click()

'Here I would like to send the list number to module DataCloset
‘before it returns the data.

  NamesList = Split(DataCloset.Data(), ",", " ", True)
  ‘followed by the rest of the code
   
End
Old african saying:
You eat an elephant one small bite at a time.
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Passing a variable to a module ?

Post by stevedee »

Doctor Watson wrote: Sunday 11th April 2021 8:27am ...Question : How can I pass a variable to a module?...
I'm not totally clear on how you want to implement this, but one approach is to create a Public variable in your module.

So maybe:-
' Module DataCloset

Public intListNimber as Integer

NamesStr$ As String
 
Public Function Data() As String
' Receive a value for x from Button1_Click() ???
  
Select Case intListNumber
Case 1
  NamesStr$ = "John,Michael,Harry,Liam,William,Oliver,James,Peter"
Case 2
  NamesStr$ = "Isabelle,Yvonne,Mary,Patricia,Olivia,Emma,Eve,Amalia,Petra,Fleur"
Case 3
  NamesStr$ = "Fido,Tiger,Bella,Ginger,Momo,Chibi"
End Select
 
Return NamesStr$
   
End
...and then in FMain:-
Public Sub Button1_Click()
 
'Here I would like to send the list number to module DataCloset
'before it returns the data.
  DataCloset.intListNumber = 3
 
  NamesList = Split(DataCloset.Data(), ",", " ", True)
  'followed by the rest of the code
    
End
...or instead of declaring a Public variable, maybe pass the list number as an argument to the Data function, something like this:-
Public Function Data(iList as integer) As String
'Receive a value for x from...
...
  
Public Sub Button1_Click()
  
'Here I would like to send the list number to module DataCloset
'before it returns the data.
  
  NamesList = Split(DataCloset.Data(3), ",", " ", True)
  'followed by the rest of the code
     
End.
I hope that is clear.
Last edited by stevedee on Tuesday 13th April 2021 6:02am, edited 1 time in total.
Doctor Watson
Posts: 84
Joined: Wednesday 22nd August 2018 7:55am

Re: Passing a variable to a module ?

Post by Doctor Watson »

Thanks Steve !
The first solution does exactly what I'm looking for.
I just forgot about about the Public declaration. Oh dear ...
Old african saying:
You eat an elephant one small bite at a time.
Post Reply