为全局变量赋值(Delphi)

发布于 2024-08-03 00:49:43 字数 317 浏览 9 评论 0原文

我正在尝试访问表单数组中的一个表单上的全局变量,我尝试使用这个:

max_forms := 3

setlength(form_array, max_forms);

form_array[1] := frm1;
form_array[2] := frm2;

if current_form > 0 then
begin
  form_array[current_form].fNumber := Number;
  form_array[current_form].ShowModal;
end;

上面的方法显然不起作用。任何帮助将不胜感激。

I am trying to access a global variable on a form that is one in an Array of Forms, I have tried using this:

max_forms := 3

setlength(form_array, max_forms);

form_array[1] := frm1;
form_array[2] := frm2;

if current_form > 0 then
begin
  form_array[current_form].fNumber := Number;
  form_array[current_form].ShowModal;
end;

The above does not work obviously. Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤星 2024-08-10 00:49:43

我猜问题是 fNumber 具有私有访问权限。

根据此字段的用途,一种解决方案可能是创建一个属性来获得写访问权限:

property Number: Integer read FNumber write FNumber;

然后您可以进行分配:

form_array[current_form].Number := Number;

关于全局变量:

如果这实际上是问题和 FNumber 是您正在谈论的“全局”变量,那么您使用了错误的词。 FNumber 是一个字段,属于一个表单。表单成员不是全局的。

查看表格的来源。如果它是由 Delphi IDE 生成的,您将在其下面找到一个变量声明:

end; // End of TForm1

var
  Form1: TForm1;

implementation

Form1 是一个真正的全局变量,因为它存在于单元的接口中以及任何类的外部,您可以从任何地方访问(一般来说这不是一件好事),对于 FNumber 您首先需要访问表单实例。

附:
我不知道您到底想要实现什么目标,但也许您可以看一下 Screen.Forms 它提供了活动表单的列表。这可能比自定义列表更适合。

I guess the problem is that fNumber has private access.

Depending on the purpose of this field, one solution could be creating a property to gain write access:

property Number: Integer read FNumber write FNumber;

Then you can do the assignment:

form_array[current_form].Number := Number;

About global variables:

If this is in fact the problem and FNumber is the "global" variable you are talking about, then you are using the wrong words. FNumber is a field and belongs to a form. Form members are not global.

Look at the source of your form. If it has been generated by the Delphi IDE you'll find a variable declaration below it:

end; // End of TForm1

var
  Form1: TForm1;

implementation

Form1 is a real global variable, because it exists in the interface of a unit and outside of any class and you can access from anywhere (not a good thing in general) as for FNumber you first need access to a form instance.

PS:
I don't know what exactly you are trying to achive, but perhaps you can take a look at Screen.Forms which provides a list of the active forms. That might be better suited than a custom list.

书信已泛黄 2024-08-10 00:49:43

更改您的代码以使用表单的 Tag 属性...它是一个便签本,供您存储整数值...或来自对象(.tag := integer(MyObject)) 和整数的任何内容...

max_forms := 3

setlength(form_array, max_forms);

form_array[1] := frm1;
form_array[2] := frm2;

if current_form > 0 then
begin
  form_array[current_form].Tag:= Number;
  form_array[current_form].ShowModal;
end;

Change your code to use the Tag property of the form...It's a scratch pad for you to store integer values...or anything from objects(.tag := integer(MyObject)) and integers...

max_forms := 3

setlength(form_array, max_forms);

form_array[1] := frm1;
form_array[2] := frm2;

if current_form > 0 then
begin
  form_array[current_form].Tag:= Number;
  form_array[current_form].ShowModal;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文