简体   繁体   English

在表单中创建Delphi对象

[英]Creating a Delphi Object in a Form

I have a Delphi Form with no component in it. 我有一个没有组件的Delphi表单。 Then, I create a unit with several classes. 然后,我创建了一个包含多个类的单元。 I've been trying to Instantiate the class and create an object in the form, but it's complaining that the class is undeclared. 我一直在尝试实例化类并在表单中创建一个对象,但它抱怨该类是未声明的。 here is the error message: 'E2003 Undeclared Identifier:TUser'. 这是错误消息:'E2003 Undeclared Identifier:TUser'。

Here is the project: 这是项目:

program Testing; 程序测试;

uses
  Forms,
  Home in 'Home.pas' {Form1},
  uUser in 'uUser.pas';

{$R *.res}

begin
  ReportMemoryLeaksOnShutdown := DebugHook <> 0;
  Application.Initialize;
  Application.Run;
end.

Here is my Empty Form: 这是我的空表格:

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.

Here is the Class that I created: 这是我创建的类:

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.

All of your classes are in the implementation section, meaning they're not visible outside the unit itself. 您的所有类都在implementation部分中,这意味着它们在单元本身之外是不可见的。 Move them to the interface section. 将它们移动到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.

Also, as a note: the only reason to descend from TComponent is to create components that will appear on the Component Palette (which means they need a Register procedure). 另外,作为注释:从TComponent下降的唯一原因是创建将出现在Component Palette上的组件(这意味着它们需要Register过程)。 If you're not designing components that will be dropped on a form at design-time, they don't need the overhead of TComponent as a ancestor - they can simply descend from TObject instead, which would appear to be the case for your TUser class. 如果你没有设计将在设计时放在表单上的组件,他们不需要TComponent作为祖先的开销 - 他们可以简单地从TObject下降,这似乎是你的TUser的情况类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM