delphi - 以其他形式使用声明的公共(public)变量时的编译器错误

标签 delphi variables public delphi-xe3

我之前声明了许多公共(public)变量,但在 Delphi-XE3 中是第一个。

我声明 public var 好,但不能在其他形式上使用它,但我不知道为什么?

我的主要联合代码:

unit uMian;

interface

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

type
  TForm1 = class(TForm)
    btnLoad: TButton;
    btnShow: TButton;
    btnEaxit: TButton;
    lvStudent: TListView;
    btnAdd: TButton;
    procedure btnEaxitClick(Sender: TObject);
    procedure btnLoadClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnAddClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations } 
  var
    IsOpen : Boolean ;

  public
    { Public declarations }
    var
      myvar : Integer ;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses uAdd;


function AppDir () : string ;
begin
  Result := ExtractFilePath(Application.ExeName);
end;

procedure TForm1.btnAddClick(Sender: TObject);
begin
  if IsOpen then
  frmAdd.ShowModal ;
end;

procedure TForm1.btnEaxitClick(Sender: TObject);
begin
  close;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if IsOpen then
  //CloseFile(DataFile);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  IsOpen := False ;
end;

....

end.

这是很好的工作,没有问题。

但是当我在其他表单 FrmAdd 上使用时,我遇到了问题:
unit uAdd;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons,
  Vcl.Samples.Spin, Vcl.ExtCtrls;

type
  TfrmAdd = class(TForm)
    lbledtName: TLabeledEdit;
    lbledtAvg: TLabeledEdit;
    seAge: TSpinEdit;
    lbleId: TLabeledEdit;
    btnCansek: TBitBtn;
    btnAdd: TBitBtn;
    procedure btnAddClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmAdd: TfrmAdd;

implementation

{$R *.dfm}

uses uMian;

procedure TfrmAdd.btnAddClick(Sender: TObject);

begin
  // problem in below 
  ShowMessage(IntToStr(TForm1.myvar)) ;
end;

end.

编译器错误:
[dcc32 错误] uAdd.pas(problem line): E2096 Method identifier expected

注意:Cnpack 给我看 myvar在变量列表中,但 Rad Studio Auto-Complete 不在。

最佳答案

myvar是对象的公共(public)变量,即类TForm1的实例.因此,您需要指定您所指的实例。但是当你写 TForm1.myvar ,您不指定任何实例,只指定类。

这就像知道每个人都有一种被称为“性”的属性,然后你问,“人类的性别是什么?”。 【哪个人类?】

解决办法是写someform.myvar相反,其中 someformTForm1 的一个实例.例如,Form1.myvar ,很可能是你想要的。请注意,您会找到 var Form1: TForm1uMain .这是声明表单变量的行:您有一个指向类对象的变量 TForm1 .

所以,相反,你问,“你的 friend 史密斯的性别是什么?”

关于delphi - 以其他形式使用声明的公共(public)变量时的编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14893758/

相关文章:

delphi - 将文字转换为 PChar/PAnsiChar

xml - XSLT 1.0 可以在 select 子句中的 xmlns 属性上使用变量吗?

java - 不可变对象(immutable对象)中的私有(private)最终字段与公共(public)最终字段(java)

reactjs - 将React项目部署到github页面面临公共(public)文件夹中图像的问题

delphi - 我应该使用哪一个来为 Delphi、NativeUInt 或 NativeInt 进行 64 位指针数学运算

delphi - 意想不到的隐式接口(interface)变量的神秘案例

c++ - 变量名中的美元符号

python中的变量正确使用

ruby-on-rails - 如何路由到robots.txt?

arrays - Delphi:TImage 数组