How to set Global variables in module?

Just starting out? Need help? Post your questions and find answers here.
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

How to set Global variables in module?

Post by SkyManager »

I want to set a global default gadget width and height,
But PB complains!! Please help.

Code: Select all

DeclareModule TestLibrary
  Global BtnWidth = 25
  Global BtnHeight = 25
  Declare CreateGgt(GgtID, GgtX = 0, GgtY = 0, GgtWidth = BtnWidth, GgtHeight = BtnHeight, Txt.s = "")
EndDeclareModule
Module TestLibrary
  Procedure CreateGgt(GgtID, GgtX = 0, GgtY = 0, GgtWidth = BtnWidth, GgtHeight = BtnHeight, Txt.s = "")
    GID = ButtonGadget(BtnGgtID, GgtX, GgtY, GgtWidth, GgtHeight, Txt)
    ProcedureReturn GID
  EndProcedure
EndModule
; =========================== TEST
use TestLibrary
OpenWindow(0, 0, 0, 100, 100, "Test")
TestLibrary::CreateGgt(1,"Btn")
Repeat Until WaitWindowEvent() = #PB_Event_CloseWindow
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: How to set Global variables in module?

Post by Demivec »

Example removed. See later post.
Last edited by Demivec on Sat Apr 21, 2018 7:26 am, edited 1 time in total.
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: How to set Global variables in module?

Post by TI-994A »

SkyManager wrote:I want to set a global default gadget width and height,
But PB complains!! Please help.

Code: Select all

DeclareModule TestLibrary
  Global BtnWidth = 25
  Global BtnHeight = 25
  Declare CreateGgt(GgtID, GgtX = 0, GgtY = 0, GgtWidth = BtnWidth, GgtHeight = BtnHeight, Txt.s = "")
EndDeclareModule
Module TestLibrary
  Procedure CreateGgt(GgtID, GgtX = 0, GgtY = 0, GgtWidth = BtnWidth, GgtHeight = BtnHeight, Txt.s = "")
    GID = ButtonGadget(BtnGgtID, GgtX, GgtY, GgtWidth, GgtHeight, Txt)
    ProcedureReturn GID
  EndProcedure
EndModule
; =========================== TEST
use TestLibrary
OpenWindow(0, 0, 0, 100, 100, "Test")
TestLibrary::CreateGgt(1,"Btn")
Repeat Until WaitWindowEvent() = #PB_Event_CloseWindow
Default values in procedures must be literals or constants; so, simply change the two globals to constants. Furthermore, even optional parameters cannot be leapfrogged; in order to define a later parameter, the earlier ones must be defined as well. The solution would be to move the oft-used optional parameters closer to the beginning.

Here's the working code:

Code: Select all

DeclareModule TestLibrary
  #BtnWidth = 25
  #BtnHeight = 25
  Declare CreateGgt(GgtID.i, Txt.s = "", GgtX.i = 0, GgtY.i = 0, GgtWidth.i = #BtnWidth, GgtHeight.i = #BtnHeight)
EndDeclareModule
Module TestLibrary
  Procedure CreateGgt(GgtID.i, Txt.s = "", GgtX.i = 0, GgtY.i = 0, GgtWidth.i = #BtnWidth, GgtHeight.i = #BtnHeight)
    GID = ButtonGadget(BtnGgtID, GgtX, GgtY, GgtWidth, GgtHeight, Txt)
    ProcedureReturn GID
  EndProcedure
EndModule
; =========================== TEST
UseModule TestLibrary
OpenWindow(0, 0, 0, 100, 100, "Test")
TestLibrary::CreateGgt(1, "Btn")
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

Re: How to set Global variables in module?

Post by SkyManager »

This will be a problem for me.

When I set the default values for width and height, I intend they CAN BE CHANGED afterward.

However, PB module only allows us to use CONSTANTS.

Could anybody suggest any way to overcome this?
forumuser
User
User
Posts: 98
Joined: Wed Apr 18, 2018 8:24 am

Re: How to set Global variables in module?

Post by forumuser »

Code: Select all

DeclareModule TestLibrary
  Declare CreateGgt(GgtID, GgtX = 0, GgtY = 0, GgtWidth = 25, GgtHeight = 25, Txt.s = "")
EndDeclareModule
Module TestLibrary
  Procedure CreateGgt(GgtID, GgtX = 0, GgtY = 0, GgtWidth = 25, GgtHeight = 25, Txt.s = "")
    GID = ButtonGadget(BtnGgtID, GgtX, GgtY, GgtWidth, GgtHeight, Txt)
    ProcedureReturn GID
  EndProcedure
EndModule
; =========================== TEST
use TestLibrary
OpenWindow(0, 0, 0, 100, 100, "Test")
TestLibrary::CreateGgt(1,"Btn")
Repeat Until WaitWindowEvent() = #PB_Event_CloseWindow
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: How to set Global variables in module?

Post by Mijikai »

U could do something like this:

Code: Select all

DeclareModule TestLibrary
  Declare.i CreateGgt(GgtX.i = 0,GgtY.i = 0,GgtWidth.i = -1,GgtHeight.i = -1,Txt.s = "")
EndDeclareModule


Module TestLibrary
  
  Procedure.i CreateGgt(GgtX.i = 0,GgtY.i = 0,GgtWidth.i = -1,GgtHeight.i = -1,Txt.s = "")
    Static Width.i = 100
    Static Height.i = 20
    If GgtWidth < 0
      GgtWidth = Width
    Else
      Width = GgtWidth
    EndIf
    If GgtHeight < 0
      GgtHeight = Height
    Else
      Height = GgtHeight
    EndIf
    ProcedureReturn ButtonGadget(#PB_Any,GgtX,GgtX,GgtWidth,GgtHeight,Txt)
  EndProcedure
  
EndModule

If OpenWindow(0, 0, 0, 100, 100, "Test",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
  TestLibrary::CreateGgt()
  Repeat 
  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
If the set Width and/or Height is smaller #Null it will retain the last/original set value.
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: How to set Global variables in module?

Post by TI-994A »

SkyManager wrote:When I set the default values for width and height, I intend they CAN BE CHANGED afterward.
Try this:

Code: Select all

DeclareModule TestLibrary
  Global BtnX = 10
  Global BtnY = 10
  Global BtnWidth = 100
  Global BtnHeight = 30
  Global BtnCaption.s = "Button"
  Declare CreateGgt(GgtID.i, Txt.s = "", GgtX.i = 0, GgtY.i = 0, GgtWidth.i = 0, GgtHeight.i = 0)
EndDeclareModule

Module TestLibrary
  Procedure CreateGgt(GgtID.i, Txt.s = "", GgtX.i = 0, GgtY.i = 0, GgtWidth.i = 0, GgtHeight.i = 0)
    
    If Txt = "" : Txt = BtnCaption : EndIf
    If GgtX = 0 : GgtX = BtnX : EndIf
    If GgtY = 0 : GgtY = BtnY : EndIf
    If GgtWidth = 0 : GgtWidth = BtnWidth : EndIf
    If GgtHeight = 0 : GgtHeight = BtnHeight : EndIf
    GID = ButtonGadget(GgtID, GgtX, GgtY, GgtWidth, GgtHeight, Txt)
    
    ProcedureReturn GID
  EndProcedure
EndModule

; =========================== TEST

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(0, 0, 0, 400, 100, "Module Gadgets", wFlags)

UseModule TestLibrary

;use the module's default values
CreateGgt(1)

;change the module's global position
BtnX = 120
BtnY = 10
CreateGgt(2, "Button 2")

;change the module's global size & caption
BtnWidth = 160
BtnHeight = 40
BtnCaption = "Button 3"
CreateGgt(3, "", 230, 50)

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: How to set Global variables in module?

Post by mk-soft »

I think is better without global variables...

Code: Select all

DeclareModule TestLibrary
  Declare SetGgtDefault(Txt.s = "", GgtX.i = #PB_Ignore, GgtY.i = #PB_Ignore, GgtWidth.i = #PB_Ignore, GgtHeight.i = #PB_Ignore)
  Declare CreateGgt(GgtID.i, Txt.s = "", GgtX.i = #PB_Ignore, GgtY.i = #PB_Ignore, GgtWidth.i = #PB_Ignore, GgtHeight.i = #PB_Ignore)
EndDeclareModule

Module TestLibrary
  
  Structure udtBtnDefault
    BtnText.s
    BtnX.i
    BtnY.i
    BtnWidth.i
    BtnHeight.i
  EndStructure
  
  Global BtnDefault.udtBtnDefault
  
  Procedure InitModule()
    With BtnDefault
      \BtnText = "Button"
      \BtnX = 10
      \BtnY = 10
      \BtnWidth = 100
      \BtnHeight = 30
    EndWith
  EndProcedure : InitModule()
  
  
  Procedure SetGgtDefault(Txt.s = "", GgtX.i = #PB_Ignore, GgtY.i = #PB_Ignore, GgtWidth.i = #PB_Ignore, GgtHeight.i = #PB_Ignore)
    
    With BtnDefault
      If Txt <> "" : \BtnText = Txt : EndIf
      If GgtX <> #PB_Ignore : \BtnX = GgtX : EndIf
      If GgtY <> #PB_Ignore : \BtnY = GgtY : EndIf
      If GgtWidth <> #PB_Ignore : \BtnWidth = GgtWidth : EndIf
      If GgtHeight <> #PB_Ignore : \BtnHeight = GgtHeight : EndIf
    EndWith
  EndProcedure
  
  Procedure CreateGgt(GgtID.i, Txt.s = "", GgtX.i = #PB_Ignore, GgtY.i = #PB_Ignore, GgtWidth.i = #PB_Ignore, GgtHeight.i = #PB_Ignore)
    
    With BtnDefault
      If Txt = "" : Txt = \BtnText : EndIf
      If GgtX = #PB_Ignore : GgtX = \BtnX : EndIf
      If GgtY = #PB_Ignore : GgtY = \BtnY : EndIf
      If GgtWidth = #PB_Ignore : GgtWidth = \BtnWidth : EndIf
      If GgtHeight = #PB_Ignore : GgtHeight = \BtnHeight : EndIf
    EndWith
    GID = ButtonGadget(GgtID, GgtX, GgtY, GgtWidth, GgtHeight, Txt)
    
    ProcedureReturn GID
  EndProcedure
EndModule

; =========================== TEST

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(0, 0, 0, 400, 100, "Module Gadgets", wFlags)

UseModule TestLibrary

;use the module's default values
CreateGgt(1)

;change the module's global position
SetGgtDefault("", 120, 10)
CreateGgt(2, "Button 2")

;change the module's global size & caption
SetGgtDefault("Button 3", #PB_Ignore, #PB_Ignore, 160, 40)
CreateGgt(3, "", 230, 50)

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

Re: How to set Global variables in module?

Post by SkyManager »

Thank you for all the suggestions.
I guess I have some ideas how to proceed.
:D
Post Reply