Pass a string reference from one unit to another

i have a global bolean variable in one unit, to pass it as a reference to the constructor in another unit i do by:

Constructor Create(var myBol:Boolean);

But it will only exist in the constructor, and die when the constructor dies. How do i make this reference stay alive in the object as long as the instance exists?

I tried this (and it did not work)

(in the interface)
var
    var gBol:Boolean;

and then in the constructor:

gBol:=myBol;

but it does not work. What is the proper way to do this?
Paer ToernellAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mlmccCommented:
You make it part of the data/variables in the object.

Can you show the code for the object?

mlmcc
Paer ToernellAuthor Commented:
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Unit2;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    gBoolean:Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
   var
     obj:TmyObjetc;
begin
     obj:=TmyObjetc.Create(gBoolean);
     gBoolean:=False;
     obj.DoIt;
     if gBoolean=True then Edit1.Text:='Success' else Edit1.Text:='Fail';
     FreeAndNil(obj);
end;

end.

unit Unit2;

interface
type

TmyObjetc=class(Tobject)
    tMyGlobalBool:boolean;
    constructor Create(var aPass:Boolean);
    Procedure DoIt;
end;

implementation

{ TmyObjetc }

constructor TmyObjetc.Create(var aPass: Boolean);
begin
      tMyGlobalBool:=aPass;
end;

procedure TmyObjetc.DoIt;
begin
       tMyGlobalBool:=True;
end;

end.

Open in new window

mlmccCommented:
You need to put it in the Public interface then reference it through the object.  Your class should not depend on the existence of a global variable.

procedure TForm1.Button1Click(Sender: TObject);
   var
     obj:TmyObjetc;
begin
     obj:=TmyObjetc.Create(gBoolean);
     gBoolean:=False;
     obj.DoIt;
     if obj.gBoolean=True then Edit1.Text:='Success' else Edit1.Text:='Fail';
     FreeAndNil(obj);
end;

Open in new window


mlmcc
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Paer ToernellAuthor Commented:
That wont do. It must be the referenced Boolean that change  its value from the object without any additional code except the one passing of the reference.
LRHGuyCommented:
If you have a global:

var gBoolean:boolean;

and a type:

type pBoolean=^boolean;

You can pass:

X:=AClass.Create(gBoolean);

when:

type
   AClass=class
      fBool:pboolean;
     constructor Create(var aBoolean:boolean);
     function CheckBool:boolean;
  end;

constructor AClass.Create(var aBoolean:boolean);
begin
  fBool:=@aBoolean;
end;

function AClass.CheckBool:boolean;
begin
  Result:=fBool^;
end;

then the class can check the current value:

if X.CheckBook then xxxxx;

when you change the global:

gboolean:=true; //or false

Maybe this is what you are looking for?
LRHGuyCommented:
And, the class change change the value with:

procedure AClass.ChangeBool(aNewValue:boolean);
begin
  fbool^:=aNewValue;
end;

If I haven't made any typing mistakes, that should change the global value.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Geert GOracle dbaCommented:
if you have a global variable, you don't have to pass anything
you can change it from inside any method, that's including a constructor or destructor
however ... other objects will not know of the change ... unless they track or are notified

if you have 40 instances of that same object, that would mean that the value can be different in each instance
luckily a boolean only has 2 distinct values ... try using an integer ... you'll have a lot of different values then

if you want 1 global value and when 1 object changes it, you want all objects to be notified of the change ...
then you need to examine the observer design pattern
http://www.felix-colibri.com/papers/design_patterns/gof_design_patterns/gof_design_patterns.html

this has a complete implementation
http://delphipatterns.blog.com/2011/03/29/observer/

when a price of a stock article (= subject) changes, alle investors (=observers) are notified of the change
Igor UL7AAjrSenior developerCommented:
I suppose that using PBoolean can be usefull. So, you will keep a reference to passed in constructor boolean variable and can modify passed variable by its referense;




  TSomeObject = (...
  protected
    FExtBoolean: PBoolean;
  public
    constructor Create(var AValue: Boolean);
   procedure SomeMethod;
 end;


constructor TSomeObject.Create(var AValue: Boolean);
begin
   ...
   FExtBoolean := @AValue;
end;


procedure TSomeObject.SomeMethod;
begin
  FExtBoolean^ := True;
end;

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.