Delphi 10 - 如何在两个表单之间共享对象?

时间:2015-10-07 12:09:09

标签: class delphi oop

我正在尝试从我创建的对象访问用户名字符串sUsername,并以不同的形式使用它。

我不想重新创建对象,否则将清除用户名字段。

如果我已经从第一个表单创建并设置了用户名,那么如何从第二个表单中的对象中检索用户名(我在哪里obj.create(sUsername)

我已经尝试创建一个名为function GetUsername : string的功能来发送结果,但它最终会在另一个表单上给我一个违规错误?有人能给我一个基本的例子,说明如何从另一个表格中发出的OOP中“调用”一个左右的值。

更新 - 对不起,这是一个我想要做的例子,但是我得到了一个错误违规,因为你可以看到我没有释放该对象,因为我想保留其他表单的值。:

**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.

第二种形式:

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.

1 个答案:

答案 0 :(得分:3)

使用任何对象,您需要决定谁拥有它。一个对象应该负责对象的生命周期管理 - 这通常是需要对象的生命周期最长的对象。例如,主窗体将拥有其子窗体所需的对象。

那个所有者对象通常应该对象注入其他需要它的依赖对象。

例如:

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;

实施为:

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;

此处,表单1取得TFoo对象FFoo的所有权角色。当它创建第二个表单时,它会向第二个表单传递对其FFoo对象的引用。

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.