Jump to content

Global variable


Volk65

Recommended Posts

As an instance of form where to store? In the global variable? in another form?
I want to get away from storing a variable in MainForm. To create an instance of a class I need to store it somewhere but not in the form of a project. That's why I want to use ThreadVar.
I need an answer to the question: Is it possible to use a global variable declared as Threadvar (so that each session sees its own variable) ?

 

Link to comment
Share on other sites

@Volk65

Global variable per session.

Here is my  kitchen :

Create an  UniDataModule  specially dedicated to these variables.

for example  name it  Tmp  (a temporary Unit) :

 

unit Tmp;

interface

uses
  SysUtils, Classes;

type
  TDM_tmp = class(TDataModule)


//*********
  private
    // variables shared by all units example :
    

    // Periode
    fD1:TDate;
    fD2:TDate;
    
    // Printing
    fPrintLine:boolean;

    // somme set errors
    ferror:boolean;
    etc...




//*********
 published

    // Periode
    property D1:TDate read fD1 write fD1;
    property D2:TDate read fD2 write fD2;
    

    // Printing 
    property PrintLine:boolean read fPrintLine write fPrintLine;

    // somme set errors
    property error:boolean read ferror write ferror;


  end;





function DM_tmp: TDM_tmp;

implementation

{$R *.dfm}

uses
  UniGUIVars, uniGUIMainModule, MainModule;

function DM_tmp: TDM_tmp;
begin
  Result := TDM_tmp(UniMainModule.GetModuleInstance(TDM_tmp));
end;

initialization
  RegisterModuleClass(TDM_tmp);

end.

 

 

And after,  on  your others Units, you can get them  or set them like :

tmp.DM_tmp.D1  :=dtpMyDateN.dateTime;
if tmp.DM_tmp.error then ....
if tmp.DM_tmp.PrintLine then...

 

It is important to use  properties  to never have  overlap variables on different sessions 

see :  http://www.unigui.com/doc/online_help/general-design-concept.htm

Regards.

  • Like 1
Link to comment
Share on other sites

Yeah, that's a good option. A few tweaks to get rid of the dependency in uses on a particular project.
Be so:

uses
  UniGUIVars, uniGUIMainModule, MainModule;

function DM_tmp: TDM_tmp;
begin
  Result := TDM_tmp(UniMainModule.GetModuleInstance(TDM_tmp));
end;

Become so:

uses
  UniGUIVars, uniGUIApplication;

function DataModule1: TDataModule1;
begin
  Result := TDM_tmp(UniApplication.UniMainModule.GetModuleInstance(TDM_tmp));
end;


Thanks for the idea.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...