I KNOW ALONE
I KNOW ALONE

Reputation: 21

Delphi 10 - How can I share an object between two forms?

I'm trying to access the Username string sUsername from an object I have created and use it in a different form.

I don't want to recreate the object, otherwise the Username field will be cleared.

How can I retrieve the username from the object in the second form if I've created and set the username from the first form (where I did obj.create(sUsername)?

I already tried to create a funtion called function GetUsername : string to send the result but It ends up giving me a violation error on the other form? Can someone give me a basic example how to "call" a value or so from the OOP that was sended from another form to it.

UPDATE - Sorry , here is a example of what I want to do but I get an error violation as you can see I didnt free the object since I want to keep the values for the other form.:

**First Form :**
unit SendUsername_u;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, clsUsername, GetUsername_u;

type
  TForm1 = class(TForm)
    edtUsername: TEdit;
    Button1: TButton;
    btnShowOtherForm: TButton;
    procedure Button1Click(Sender: TObject);
    procedure btnShowOtherFormClick(Sender: TObject);
  private
    objUsername: Ttest;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnShowOtherFormClick(Sender: TObject);
begin
  Form1.Hide;
  form2.show;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 objUsername :=  Ttest.create(edtUsername.Text);
end;

end.

Second Form :

unit GetUsername_u;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,  clsUsername;

type
  TForm2 = class(TForm)
    btnGetUsername: TButton;
    procedure btnGetUsernameClick(Sender: TObject);
  private
    objUsername: ttest;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.btnGetUsernameClick(Sender: TObject);
begin
  ShowMessage(objUsername.GetUsername);
end;

end

OOP/Class :

unit clsUsername;

interface

uses
  SysUtils;

type
  Ttest = class(TObject)

  private
    fUsername: string;
  public

    constructor create(sUsername: string);
    function GetUsername: string;
  end;

implementation

{ Ttest }

constructor Ttest.create(sUsername: string);
begin
  fUsername := sUsername;
end;

function Ttest.GetUsername: string;
begin
  Result := fUsername;
end;

end.

Upvotes: 0

Views: 2053

Answers (1)

J...
J...

Reputation: 31463

With any object you need to decide who owns it. One object should be responsible for the object's lifetime management - this is usually the object with the longest lifetime that requires the object. A main form, for example, would own objects that are needed by its sub-forms.

That owner object should usually then inject the object into other dependent objects which require it.

For example :

unit Unit1;

interface

uses
  Windows, SysUtils, Classes, Forms, StdCtrls, Vcl.Controls, Vcl.ExtCtrls;

type

  TFoo = class
    private
      FBarStr : string;
    public
      property Bar : string read FBarStr write FBarStr;
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    private
      FFoo : TFoo;
    public
 end;

implemented as :

implementation

{$R *.dfm}

uses
  Unit2;

procedure TForm1.Button1Click(Sender: TObject);
var
  LForm2 : TForm2;
begin
  LForm2 := TForm2.Create(nil);
  try
    LForm2.Foo := FFoo;  // << Here you are passing a reference to FFoo
    LForm2.ShowModal;    //    to your second form
  finally
    LForm2.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FFoo := TFoo.Create;
  FFoo.Bar := 'bar';   // could be your username, etc...
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FFoo.Free;
end;

Here form 1 takes the ownership role of the TFoo object, FFoo. When it creates the second form it passes the second form a reference to its FFoo object.

unit Unit2;

interface

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

type
  TForm2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    FFoo : TFoo;
  public
    property Foo : TFoo read FFoo write FFoo;
  end;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
  if Assigned(FFoo) then ShowMessage(FFoo.Bar);
end;

end.

Upvotes: 3

Related Questions