Jump to content

Unimainmodule Global Variable


donlego

Recommended Posts

  • 2 years later...

I have found this thread and don't understand the problem.

I know that for each session a TuniMainModule instance is created.

So if i create some variables in the unimainmodule, isn't it safe to read /write these variables ?

example

TUniMainModule = class(TUniGUIMainModule)

private
    { Private declarations }
    Fthevar:integer;

  published
    property thevar:integer read Fthevar write Fthevar;
...

If somewhere in the app the code below is execute

UniMainModule.thevar:=newvalue;

Isn't it safe ?

thanks

Link to comment
Share on other sites

28 minutes ago, Roberto Nicchi said:

I have found this thread and don't understand the problem.

I know that for each session a TuniMainModule instance is created.

So if i create some variables in the unimainmodule, isn't it safe to read /write these variables ?

example


TUniMainModule = class(TUniGUIMainModule)

private
    { Private declarations }
    Fthevar:integer;

  published
    property thevar:integer read Fthevar write Fthevar;
...

If somewhere in the app the code below is execute


UniMainModule.thevar:=newvalue;

Isn't it safe ?

thanks

 

Hello,

Your Code is Safe.

There is no problem when  using  properties.

in your example,  thevar  is visible for only the current session.

(Read / Write)   =  No problem.

Link to comment
Share on other sites

3 minutes ago, Abaksoft said:

 

Hello,

Your Code is Safe.

There is no problem when  using  properties.

in your example,  thevar  is visible for only the current session.

(Read / Write)   =  No problem.

If i had created the variable as public and didn't use the property, it wasn't correct ?

thanks a lot.

Link to comment
Share on other sites

2 hours ago, Roberto Nicchi said:

If i had created the variable as public and didn't use the property, it wasn't correct ?

thanks a lot.

 

As public (without property) = Not Safe  !

You can read this from Online Documentation :

 

"For example, one could define TUserInfo as a global variable in MainModule:

 unit MainModule;

 interface
...
var
  UserInfo: TUserInfo;

 And access it in MainForm:

procedure TMainForm.UniFormCreate(Sender: TObject);

begin
  UniLabel1.Caption := UserInfo.Name + ' ' + UserInfo.Surname;
end;

 

Above code actually works during initial tests because, as long as there is only one active session, UserInfo will not be shared, and the application seems to behave correctly! However, as soon as multiple sessions logs in, the application starts to behave in a strange way."

 

From :

http://www.unigui.com/doc/online_help/index.html?general-design-concept.htm

 

I never use public variable. Always properties to share variables beween Forms /Frames (on the current session).

And to do best, i create a dedicated UniDataModule, only to put shared properties.

 

http://forums.unigui.com/index.php?/topic/11974-global-variable/&do=findComment&comment=63879

 

Link to comment
Share on other sites

1 hour ago, Abaksoft said:

 

As public (without property) = Not Safe  !

You can read this from Online Documentation :

 

"For example, one could define TUserInfo as a global variable in MainModule:

 unit MainModule;

 interface
...
var
  UserInfo: TUserInfo;

 And access it in MainForm:

procedure TMainForm.UniFormCreate(Sender: TObject);

begin
  UniLabel1.Caption := UserInfo.Name + ' ' + UserInfo.Surname;
end;

 

Above code actually works during initial tests because, as long as there is only one active session, UserInfo will not be shared, and the application seems to behave correctly! However, as soon as multiple sessions logs in, the application starts to behave in a strange way."

 

From :

http://www.unigui.com/doc/online_help/index.html?general-design-concept.htm

 

I never use public variable. Always properties to share variables beween Forms /Frames (on the current session).

And to do best, i create a dedicated UniDataModule, only to put shared properties.

 

http://forums.unigui.com/index.php?/topic/11974-global-variable/&do=findComment&comment=63879

 

In the example i see that the variable UserInfo is created inside the unit but OUTSIDE the class definition. That way for sure can't work in a UNIGUI application because the variable is common to all session. But creating the variables (public) inside the mainmodule should be in my mind the same than using a property and a private variable. Probably there's a tecnical thing that i miss. Anyway i'll do as suggested and use properties. I guess it works also if the variable is a complex object. For example if i need a Tstringlist. The only thing i have to do more is create it in the oncreate event of the unimainmodule.

 

TUniMainModule = class(TUniGUIMainModule)

private
    { Private declarations }
    Fsl:TStringList;

  published
    property sl:TStringList read Fsl write Fsl;
...

thanks

Link to comment
Share on other sites

  • 4 weeks later...

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...