在表单中创建 Delphi 对象
Creating a Delphi Object in a Form
我有一个 Delphi 表单,其中没有任何组件。然后,我创建了一个包含多个 classes 的单元。我一直在尝试实例化 class 并在表单中创建一个对象,但它抱怨 class 未声明。这是错误消息:'E2003 Undeclared Identifier:TUser'。
这是项目:
程序测试;
uses
Forms,
Home in 'Home.pas' {Form1},
uUser in 'uUser.pas';
{$R *.res}
begin
ReportMemoryLeaksOnShutdown := DebugHook <> 0;
Application.Initialize;
Application.Run;
end.
这是我的空表格:
unit Home;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, uUser;
type
TForm1 = class(TForm)
private
{ Private declarations }
protected
public
{ Public declarations }
u : TUser; //It's complaining about TUser. I can right click on
TUser, and it will take me to the class declaration.
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
end.
这是我创建的 Class:
unit uUser;
interface
uses
classes,SysUtils,Dialogs;
implementation
type
TAddress = class
private
FStreetAddress : string;
FCity : string ;
FState : string;
FZipCode : string;
procedure setStreetAddress(const Value : string);
procedure setCity(const Value : string);
procedure setState(const Value : string);
procedure setZipCode(const Value : string);
protected
public
property StreetAddress : string read FStreetAddress write setStreetAddress;
property City : string read FCity write setCity;
property State : string read FState write setState;
property ZipCode : string read FZipCode write setZipCode;
end;
type
TPermanentAdddress = class (TAddress)
private
FStartDate : string;
FEndDate : string;
procedure setStartDate(const Value : string);
procedure setEndDate(const Value : string);
protected
public
property StartDate : string read FStartDate write setStartDate;
property EndDate : string read FEndDate write setEndDate ;
end;
type
TUser = class(TComponent)
private
FFirstName : string;
FAddress : TPermanentAdddress;
procedure setFirstName(const Value : string);
procedure setAddress(const Value : TPermanentAdddress);
protected
public
constructor Create(); reintroduce; overload;
destructor Destroy(); override;
property FirstName : string read FFirstName write setFirstName;
property Address : TPermanentAdddress read FAddress write setAddress;
end;
procedure TAddress.setStreetAddress(const Value : string);
begin
FStreetAddress := value;
end;
procedure TAddress.setCity(const Value : string);
begin
FCity := Value;
end;
procedure TAddress.setState(const Value : string);
begin
FState := Value;
end;
procedure TAddress.setZipCode(const Value : string);
begin
FZipCode := Value;
end;
//Permanent Address
procedure TPermanentAdddress.setStartDate(const Value : string);
begin
FStartDate := value;
end;
procedure TPermanentAdddress.setEndDate(const Value : string);
begin
FEndDate := Value;
end;
//tvxpatient
procedure TUser.setFirstName(const Value : string);
begin
FFirstName := Value;
end;
procedure TUser.setAddress(const Value : TPermanentAdddress);
begin
FAddress := Value;
end;
constructor TUser.Create();
begin
FAddress := TPermanentAdddress.Create;
end;
destructor TUser.Destroy();
begin
//FAddress.Free;
end;
end.
您所有的 classes 都在 implementation
部分,这意味着它们在单元本身之外是不可见的。将它们移至 interface
部分。
unit uUser;
interface
uses
classes,SysUtils,Dialogs;
interface
type
TAddress = class
private
FStreetAddress : string;
FCity : string ;
FState : string;
FZipCode : string;
procedure setStreetAddress(const Value : string);
procedure setCity(const Value : string);
procedure setState(const Value : string);
procedure setZipCode(const Value : string);
protected
public
property StreetAddress : string read FStreetAddress write setStreetAddress;
property City : string read FCity write setCity;
property State : string read FState write setState;
property ZipCode : string read FZipCode write setZipCode;
end;
type
TPermanentAdddress = class (TAddress)
private
FStartDate : string;
FEndDate : string;
procedure setStartDate(const Value : string);
procedure setEndDate(const Value : string);
protected
public
property StartDate : string read FStartDate write setStartDate;
property EndDate : string read FEndDate write setEndDate ;
end;
type
TUser = class(TComponent)
private
FFirstName : string;
FAddress : TPermanentAdddress;
procedure setFirstName(const Value : string);
procedure setAddress(const Value : TPermanentAdddress);
protected
public
constructor Create(); reintroduce; overload;
destructor Destroy(); override;
property FirstName : string read FFirstName write setFirstName;
property Address : TPermanentAdddress read FAddress write setAddress;
end;
implementation
procedure TAddress.setStreetAddress(const Value : string);
begin
FStreetAddress := value;
end;
procedure TAddress.setCity(const Value : string);
begin
FCity := Value;
end;
procedure TAddress.setState(const Value : string);
begin
FState := Value;
end;
procedure TAddress.setZipCode(const Value : string);
begin
FZipCode := Value;
end;
//Permanent Address
procedure TPermanentAdddress.setStartDate(const Value : string);
begin
FStartDate := value;
end;
procedure TPermanentAdddress.setEndDate(const Value : string);
begin
FEndDate := Value;
end;
//tvxpatient
procedure TUser.setFirstName(const Value : string);
begin
FFirstName := Value;
end;
procedure TUser.setAddress(const Value : TPermanentAdddress);
begin
FAddress := Value;
end;
constructor TUser.Create();
begin
FAddress := TPermanentAdddress.Create;
end;
destructor TUser.Destroy();
begin
//FAddress.Free;
end;
end.
此外,请注意:从 TComponent
下降的唯一原因是创建将出现在组件面板上的组件(这意味着它们需要一个 Register
过程)。如果您不设计将在设计时放置在表单上的组件,则它们不需要 TComponent
作为祖先的开销 - 它们可以简单地从 TObject
继承,这将您的 TUser
class.
似乎就是这种情况
我有一个 Delphi 表单,其中没有任何组件。然后,我创建了一个包含多个 classes 的单元。我一直在尝试实例化 class 并在表单中创建一个对象,但它抱怨 class 未声明。这是错误消息:'E2003 Undeclared Identifier:TUser'。
这是项目:
程序测试;
uses
Forms,
Home in 'Home.pas' {Form1},
uUser in 'uUser.pas';
{$R *.res}
begin
ReportMemoryLeaksOnShutdown := DebugHook <> 0;
Application.Initialize;
Application.Run;
end.
这是我的空表格:
unit Home;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, uUser;
type
TForm1 = class(TForm)
private
{ Private declarations }
protected
public
{ Public declarations }
u : TUser; //It's complaining about TUser. I can right click on
TUser, and it will take me to the class declaration.
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
end.
这是我创建的 Class:
unit uUser;
interface
uses
classes,SysUtils,Dialogs;
implementation
type
TAddress = class
private
FStreetAddress : string;
FCity : string ;
FState : string;
FZipCode : string;
procedure setStreetAddress(const Value : string);
procedure setCity(const Value : string);
procedure setState(const Value : string);
procedure setZipCode(const Value : string);
protected
public
property StreetAddress : string read FStreetAddress write setStreetAddress;
property City : string read FCity write setCity;
property State : string read FState write setState;
property ZipCode : string read FZipCode write setZipCode;
end;
type
TPermanentAdddress = class (TAddress)
private
FStartDate : string;
FEndDate : string;
procedure setStartDate(const Value : string);
procedure setEndDate(const Value : string);
protected
public
property StartDate : string read FStartDate write setStartDate;
property EndDate : string read FEndDate write setEndDate ;
end;
type
TUser = class(TComponent)
private
FFirstName : string;
FAddress : TPermanentAdddress;
procedure setFirstName(const Value : string);
procedure setAddress(const Value : TPermanentAdddress);
protected
public
constructor Create(); reintroduce; overload;
destructor Destroy(); override;
property FirstName : string read FFirstName write setFirstName;
property Address : TPermanentAdddress read FAddress write setAddress;
end;
procedure TAddress.setStreetAddress(const Value : string);
begin
FStreetAddress := value;
end;
procedure TAddress.setCity(const Value : string);
begin
FCity := Value;
end;
procedure TAddress.setState(const Value : string);
begin
FState := Value;
end;
procedure TAddress.setZipCode(const Value : string);
begin
FZipCode := Value;
end;
//Permanent Address
procedure TPermanentAdddress.setStartDate(const Value : string);
begin
FStartDate := value;
end;
procedure TPermanentAdddress.setEndDate(const Value : string);
begin
FEndDate := Value;
end;
//tvxpatient
procedure TUser.setFirstName(const Value : string);
begin
FFirstName := Value;
end;
procedure TUser.setAddress(const Value : TPermanentAdddress);
begin
FAddress := Value;
end;
constructor TUser.Create();
begin
FAddress := TPermanentAdddress.Create;
end;
destructor TUser.Destroy();
begin
//FAddress.Free;
end;
end.
您所有的 classes 都在 implementation
部分,这意味着它们在单元本身之外是不可见的。将它们移至 interface
部分。
unit uUser;
interface
uses
classes,SysUtils,Dialogs;
interface
type
TAddress = class
private
FStreetAddress : string;
FCity : string ;
FState : string;
FZipCode : string;
procedure setStreetAddress(const Value : string);
procedure setCity(const Value : string);
procedure setState(const Value : string);
procedure setZipCode(const Value : string);
protected
public
property StreetAddress : string read FStreetAddress write setStreetAddress;
property City : string read FCity write setCity;
property State : string read FState write setState;
property ZipCode : string read FZipCode write setZipCode;
end;
type
TPermanentAdddress = class (TAddress)
private
FStartDate : string;
FEndDate : string;
procedure setStartDate(const Value : string);
procedure setEndDate(const Value : string);
protected
public
property StartDate : string read FStartDate write setStartDate;
property EndDate : string read FEndDate write setEndDate ;
end;
type
TUser = class(TComponent)
private
FFirstName : string;
FAddress : TPermanentAdddress;
procedure setFirstName(const Value : string);
procedure setAddress(const Value : TPermanentAdddress);
protected
public
constructor Create(); reintroduce; overload;
destructor Destroy(); override;
property FirstName : string read FFirstName write setFirstName;
property Address : TPermanentAdddress read FAddress write setAddress;
end;
implementation
procedure TAddress.setStreetAddress(const Value : string);
begin
FStreetAddress := value;
end;
procedure TAddress.setCity(const Value : string);
begin
FCity := Value;
end;
procedure TAddress.setState(const Value : string);
begin
FState := Value;
end;
procedure TAddress.setZipCode(const Value : string);
begin
FZipCode := Value;
end;
//Permanent Address
procedure TPermanentAdddress.setStartDate(const Value : string);
begin
FStartDate := value;
end;
procedure TPermanentAdddress.setEndDate(const Value : string);
begin
FEndDate := Value;
end;
//tvxpatient
procedure TUser.setFirstName(const Value : string);
begin
FFirstName := Value;
end;
procedure TUser.setAddress(const Value : TPermanentAdddress);
begin
FAddress := Value;
end;
constructor TUser.Create();
begin
FAddress := TPermanentAdddress.Create;
end;
destructor TUser.Destroy();
begin
//FAddress.Free;
end;
end.
此外,请注意:从 TComponent
下降的唯一原因是创建将出现在组件面板上的组件(这意味着它们需要一个 Register
过程)。如果您不设计将在设计时放置在表单上的组件,则它们不需要 TComponent
作为祖先的开销 - 它们可以简单地从 TObject
继承,这将您的 TUser
class.