问题描述
与Delphi有关...
As it pertains to Delphi...
当声明某种类型的变量时,是否将其初始化为该类型的对象?还是必须为变量分配一个返回该类型对象的表达式?
When a variable is declare of a certain type, is it initialized to an OBJECT of that type? Or must the variable be assigned an expression which returns an object of that type?
我来自强大的Java背景.我要问的是这个...在Java中,假设您声明了一个名为Orange的用户定义类型的实例变量.看起来像这样:
I'm coming from a strong Java background. What I mean to ask is this...In Java, say you declare an instance variable of a user defined type named Orange. Which would look like this:
private Orange _fruit;
在实际分配Orange类的实例之前,变量_fruit仍保留对null的引用,如下所示:
The variable _fruit still holds a reference to null until actually assigned an instance of the Orange class, like this:
_fruit = new Orange();
在Delphi中,如果我声明一个TForm类型的变量,像这样:
In Delphi if I declare a variable of type TForm, like this:
var
Form : TForm;
表格是否已初始化为TForm对象?还是还是零?
Is Form initlized to a TForm object? Or is it still nil?
我问是因为在尝试编译如下所示的一小段代码时出现错误:
I'm asking because I'm getting an error when trying to compile a small bit of code which is showen below:
这是主要单位:
unit Main;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils,
System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Second;
type
TForm1 = class(TForm)
ShowForm2: TButton;
procedure ShowForm2Click(Sender: TObject);
end;
var
Form1: TForm1;
SecondForm : TSecondForm;
implementation
{$R *.dfm}
procedure TForm1.ShowForm2Click(Sender: TObject);
begin
SecondForm.ShowModal;
end;
end.
这是第二个单位:
unit Second;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls;
type
TSecondForm = class(TForm)
Label1: TLabel;
end;
var
SecondForm: TSecondForm;
implementation
{$R *.dfm}
end.
我尝试编译时遇到的错误恰恰是:模块'Multiple.exe'中地址005B17F9的访问冲突.读取了地址00000000."我以为是因为我不以某种方式在Main单元中初始化变量SecondForm?但是,我试图在ShowForm2Click过程中放置"SecondForm.Create",但出现了同样的错误.我是否因为未分配SecondForm而收到此错误?是否需要初始化?是吗?
The error I'm getting when I try to compile is exactly: "Access violation at address 005B17F9 in module 'Multiple.exe'. Read of address 00000000." I was thinking that it's because I don't somehow initialize the variable SecondForm in unit Main? However, I tried to place 'SecondForm.Create' in the ShowForm2Click procedure and I get the same error. Am I get this error because SecondForm is unassigned? Does it need to be initialized? Or is it?
注意:我刚来德尔福三天了.请为此考虑.
Note:I'm three days new to Delphi. Please be considerate of that.
推荐答案
还请注意, unit Second
和 unit Main
都声明了全局变量
also note that unit Second
and unit Main
both declare a global variable
SecondForm : TSecondForm;
对于主机,您的主机将隐藏在 unit Second
中声明的 SecondForm
变量(即使它在 uses
子句).对于Delphi VCL Forms应用程序,如果 SecondForm
是自动创建的表单,则在 unit Second
中声明的 SecondForm
将不被使用. nil
,实际上已经创建并为其分配了 TSecondForm
实例,但是 unit Main
将无法访问它,因为它声明了一个全局名称(与所有引用类型一样,将为 nil
,直到您对其进行操作为止).
In the case of the main unit your main unit will be hiding the SecondForm
variable declared in unit Second
(even though it lists it in the uses
clause). In the case of a Delphi VCL Forms application, if the SecondForm
is an auto-create form then the SecondForm
declared in unit Second
will not be nil
and will in fact already have an instance of TSecondForm
created and assigned to it, but this will be inaccessible by unit Main
because it declares a global of the same name (which, like all reference types, will be nil
until you do something with it).
简而言之,最好不要在 unit Main
中声明一个全局 SecondForm:TSecondForm
-调用它以其他方式,否则请使用在 unit中声明的全局第二
.如果 SecondForm
是自动创建的表单(默认行为),那么如果您只是未在 unit Main
SecondForm ,则上述代码将起作用>-否则,您仍然必须实例化 SecondForm
.
In short, it's probably best not to declare a global SecondForm : TSecondForm
in unit Main
- call it something else or else use the global declared in unit Second
. If SecondForm
is an auto-create form (default behaviour) then the above code would work if you simply did not re-declare SecondForm
in unit Main
- if not you would still have to instantiate SecondForm
.
VCL表单是自动自动创建的表单.检查菜单:
VCL Forms are automatically auto-create forms unless you otherwise specify. Check menu:
Project > Options > Forms
查看或更改哪些表单将自动将一个实例分配给其IDE生成的全局变量.
to see or change which forms will automatically have an instance assigned to their IDE-generated globals.
这篇关于了解Delphi变量声明/初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!