Delphi Unit局部变量-如何使每个实例唯一?

发布于 2024-09-01 11:53:32 字数 1243 浏览 5 评论 0原文

在下面的单元中,我在实现部分声明了一个变量 - 该单元本地的变量。我还有一个在 TYPE 部分中声明的过程,它接受一个参数并将该参数分配给相关的局部变量。此 TFrame 的每个实例都通过 passMeTheVar 传递一个唯一的变量。

我希望它做的是让框架的每个实例保留该变量的自己的版本,与其他变量不同,并使用它来定义它的操作方式。然而,似乎发生的情况是所有实例都使用相同的值,即使我显式地向每个实例传递一个不同的变量。

即:

Unit FlexibleUnit;
interface
uses
//the uses stuff
type
TFlexibleUnit=class(TFrame)
   //declarations including
   procedure makeThisInstanceX(passMeTheVar:integer);
private
//
public
//
end;

implementation
uses //the uses
var myLocalVar;

procedure makeThisInstanceX(passMeTheVar:integer);
begin
myLocalVar:=passMeTheVar;
end;

//other procedures using myLocalVar 
//etc to the 
end;

现在在另一个表单中的某个地方,我已将此框架放到“设计”窗格中,有时在一个表单上放置两个这样的框架,并在适当的位置声明它,等等。每个框架都是唯一的:

ThisFlexibleUnit : TFlexibleUnit;
ThatFlexibleUnit : TFlexibleUnit;

当我执行以下操作时:

ThisFlexibleUnit.makeThisInstanceX(var1); //want to behave in way "var1"
ThatFlexibleUnit.makeThisInstanceX(var2); //want to behave in way "var2"

似乎它们都共享相同的变量“myLocalVar”。

原则上我这样做错了吗?如果这是正确的方法,那么就需要调试我所拥有的内容(太大而无法发布),但如果这原则上不正确,那么有没有办法做我所建议的事情?

编辑:

好的,所以这里学到的教训是类定义就是这样。许多类可以放在一个单元中,并且 Type 部分中所有类的所有实例共享该单元的实现部分。

In the unit below I have a variable declared in the IMPLEMENTATION section - local to the unit. I also have a procedure, declared in the TYPE section which takes an argument and assigns that argument to the local variable in question. Each instance of this TFrame gets passed a unique variable via passMeTheVar.

What I want it to do is for each instance of the frame to keep its own version of that variable, different from the others, and use that to define how it operates. What seems to be happening, however, is that all instances are using the same value, even if I explicitly pass each instance a different variable.

ie:

Unit FlexibleUnit;
interface
uses
//the uses stuff
type
TFlexibleUnit=class(TFrame)
   //declarations including
   procedure makeThisInstanceX(passMeTheVar:integer);
private
//
public
//
end;

implementation
uses //the uses
var myLocalVar;

procedure makeThisInstanceX(passMeTheVar:integer);
begin
myLocalVar:=passMeTheVar;
end;

//other procedures using myLocalVar 
//etc to the 
end;

Now somewhere in another Form I've dropped this Frame onto the Design pane, sometimes two of these frames on one Form, and have it declared in the proper places, etc. Each is unique in that :

ThisFlexibleUnit : TFlexibleUnit;
ThatFlexibleUnit : TFlexibleUnit;

and when I do a:

ThisFlexibleUnit.makeThisInstanceX(var1); //want to behave in way "var1"
ThatFlexibleUnit.makeThisInstanceX(var2); //want to behave in way "var2"

it seems that they both share the same variable "myLocalVar".

Am I doing this wrong, in principle? If this is the correct method then it's a matter of debugging what I have (which is too huge to post) but if this is not correct in principle then is there a way to do what I am suggesting?

EDIT:

Ok, so the lesson learned here is that the class definition is just that. Many classes can go in one unit and all instances of all classes in the Type section share the implementation section of the unit.

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

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

发布评论

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

评论(3

白首有我共你 2024-09-08 11:53:32

myLocalVar 是一个全局变量,但仅在单元内可见。

局部变量将位于过程/函数中,就像

procedure makeThisInstanceX(passMeTheVar: integer);
var
  myLocalVar: Integer;
begin
  myLocalVar := passMeTheVar;
end;

您想要一个实例变量一样,即每个帧都有自己的副本,将其放入类中:

type
  TFlexibleUnit = class(TFrame)
     procedure makeThisInstanceX(passMeTheVar:integer);
  private
    myLocalVar: Integer;
  ...
  end;

myLocalVar is a global variable, but only visible within the unit.

A local variable would be in a procedure/function, like

procedure makeThisInstanceX(passMeTheVar: integer);
var
  myLocalVar: Integer;
begin
  myLocalVar := passMeTheVar;
end;

if you want an instance variable, that is each frame has its own copy, put it in the class:

type
  TFlexibleUnit = class(TFrame)
     procedure makeThisInstanceX(passMeTheVar:integer);
  private
    myLocalVar: Integer;
  ...
  end;
无远思近则忧 2024-09-08 11:53:32

您将 makeThisInstanceX 方法作为类(静态)方法调用,而不是创建该类的实例并将其作为对象方法调用。看一下这个参考:

http://oreilly.com/catalog/delphi/chapter /ch02.html

You are calling the makeThisInstanceX method as a class (static) method rather than creating an instance of the class and calling it as an object method. Take a look at this reference:

http://oreilly.com/catalog/delphi/chapter/ch02.html

桜花祭 2024-09-08 11:53:32

框架/单元/类/控制

我赞扬你为改进代码而做出的英勇尝试。然而,从你的问题和评论来看,我很遗憾地告诉你,你的理解非常有限。

框架不是一个单元,也不是一个类。框架是一个类,但并非每个类都是框架。框架是一个控件,但并非每个控件都是框架。单元具有接口和实现(以及初始化和终止)部分。类有私有和公共(以及受保护和已发布)部分。

我放入最后一段并不是为了尝试教学,而是为了让您衡量您的理解水平。 Delphi 开发人员应该对这段文字没有任何问题。我并不是想让你感觉不好或炫耀——只是想提供帮助。也许 Stack Overflow 目前不适合您。

作为第一次学习 Delphi 的人,我可能会对一些看似多余的功能感到困惑。但该产品有着悠久的历史,每次添加在添加时都是有意义的。当您一次只需要学习一首曲子时,学习起来也更容易。

frame / unit / class / control

I applaud your heroic attempt to better the code. However, judging by your questions and comments I regret to inform you that your understanding is very limited.

A frame is not a unit which is not a class. A frame is a class but not every class is a frame. A frame is a control but not every control is a frame. Units have interface and implementation (and initialization and finalization) sections. Classes have private and public (and protected and published) parts.

I did not put the last paragraph in to try to teach but to allow you to gauge your understanding level. A Delphi developer ought to have no problem with the paragraph. I'm not trying to make you feel bad or to show off - just trying to help. Perhaps Stack Overflow is not the right tool for you at this time.

As somebody just learning Delphi for the first time, I might be confused by some of the seemingly redundant features. But the product has a long history and each addition made sense at the time it was added. It was also easier to learn when you only had to learn it a piece at a time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文