为全局变量赋值(Delphi)
我正在尝试访问表单数组中的一个表单上的全局变量,我尝试使用这个:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜问题是
fNumber
具有私有访问权限。根据此字段的用途,一种解决方案可能是创建一个属性来获得写访问权限:
然后您可以进行分配:
关于全局变量:
如果这实际上是问题和
FNumber
是您正在谈论的“全局”变量,那么您使用了错误的词。FNumber
是一个字段,属于一个表单。表单成员不是全局的。查看表格的来源。如果它是由 Delphi IDE 生成的,您将在其下面找到一个变量声明:
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:
Then you can do the assignment:
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:
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 forFNumber
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.更改您的代码以使用表单的 Tag 属性...它是一个便签本,供您存储整数值...或来自对象(.tag := integer(MyObject)) 和整数的任何内容...
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...