“标签”的目的是什么? Delphi VCL组件的属性?

发布于 2024-08-10 19:25:20 字数 117 浏览 2 评论 0原文

Delphi VCL 组件的“Tag”属性有什么特定用途吗? 我在 Google 上搜索了一些使用它作为“颜色”属性或使用该值作为指针地址的示例,但是使用它是“好习惯”,还是被认为是“坏习惯”,因为它与UI 的程序逻辑?

Is there any specific purpose for the 'Tag' property of Delphi VCL components?
I have Googled a few examples using it as, for example, a 'color' property or using the value as a pointer address, but is it 'good practice' to use it, or is it considered 'bad practice' as it ties the program logic to the UI?

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

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

发布评论

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

评论(8

∝单色的世界 2024-08-17 19:25:20

“标签”属性作为“货物容器”,用于您可能想要用它做的任何事情。

当您有许多相似的组件共享一个事件处理程序时,它经常用于事件处理程序中。事件处理程序可以找到其调用者,然后查询其标记值以获取有关其应该执行的操作的更多信息。

编辑:

示例:计算器应用程序可能会用各自的数字标记数字按钮......愚蠢且不完整的示例,但您明白了。然后,事件处理程序可以直接从标签中提取要添加到显示和累加器中的数字,而不必找出哪个按钮的用途。

The "tag" property is there as a "cargo container" for whatever you might want to do with it.

Something it's often used for is in event handlers when you have a lot of similar components sharing one event handler. The event handler can find its caller and then query its tag value to get some more information about what it's supposed to be acting on.

EDIT:

Example: A calculator app might tag the number buttons with their respective numbers... silly and incomplete example, but you get the idea. The event handler could then pull the number to add into the display and accumulator right out of the tag instead of having to go figure out which button is meant to do what.

今天小雨转甜 2024-08-17 19:25:20

这是一个向任何组件添加信息的地方,即使您没有该组件的源代码。应谨慎使用它,因为每个组件只能将其用于一个目的。因此,图书馆永远不应该使用它。

It is a place to add a piece of information to any component, even if you don't have the source for that component. It should be used carefully, because you can use it for only one purpose per component. For that reason Libraries should never use it.

意犹 2024-08-17 19:25:20

我对 Tag 属性有一些基本问题。
嗯,不完全是这个属性本身,因为它按预期工作。

一般来说,我认为使用任何通用/通用/多用途变量都是“不好的做法”。
它们在调试期间很有用,但在生产/关键任务环境中非常有害。
它们降低了代码的可读性和可理解性,因为没有人知道“标签”命名的属性或属性的作用。你当然知道为什么要使用这个变量。但迟早你会忘记(我知道你会的)并且依赖这个值会让事情变得更加复杂。
这就是为什么我们应该正确命名每个变量和属性,以帮助我们理解代码的作用。

使用 Tag 属性只是避免实现可理解且编写良好的代码的一种解决方法/快捷方式。
这就是练习,而且会让人上瘾。
下次您需要存储绑定到组件的新整数值时,您将使用 Tag 属性,而不考虑任何其他方式来存储所需的值。
在 Tag 属性中存储指针是一个可怕的想法:每次调试指针时都必须强制转换该值。

告诉我:您有多少次发现自己处于这样的情况:您想要在 Tag 属性中存储新值,但您意识到该属性已用于不同的目的(如果每个属性中都有一个“Tag2”属性就好了)成分...)。

I have some fundamental problems with the Tag property.
Well not exactly this property itself because it works as intended.

In general I consider using any universal/general/multi-purpose variables as a 'bad practice'.
They can be useful during debugging but are very harmful in production/mission critical environment.
They reduce code readability and understandability because nobody knows what a 'Tag' named attribute or property does. Of course you know why you are using this variable. But sooner or later you will forget (I know you will) and relying on this value makes anything more complicated.
This is why we should properly name every variable and property to help us understand what the code does.

Using Tag property is just a workaround/shortcut to avoid implementing understandable and well written code.
This is the PRACTICE and it is addictive.
Next time you need to store a new integer value bound to a component you will use the Tag property without considering any other way to store the desired values.
And storing a pointer in Tag property is a horrible idea: you have to cast this value every time you debug pointers.

Tell me: how many times did you find yourself in a situation where you wanted to store a new value in the Tag property but you realized this property is already used for a different purpose (if only there would be a 'Tag2' property in every component...).

债姬 2024-08-17 19:25:20

正如其他人所说,这是一个放置任何东西的地方。通常,当通过对象引用或指针关联两个对象时,这会派上用场。该标签的大小正好可以容纳指针,因此,如果您需要将一个对象与列表框中的项目绑定在一起,那么它就变得非常简单。

As others have said, it's a place to put anything. Typically this comes in handy when associating two objects via an object reference or pointer. The tag happens to be perfectly sized to hold a pointer, so if you need to, say, keep an object tied to an item in a listbox, it becomes pretty straightforward.

无法回应 2024-08-17 19:25:20

它还可以用于分组目的,假设您想要访问具有特定标记值的所有组件,而不管组件的类型如何。

Also it can be used for grouping purposes, say you'd want to access all components with a specific tag value, regardless of the component's type.

不忘初心 2024-08-17 19:25:20

这很棒!免费赠品。我一直使用它来存储与该对象相关的另一条信息。

我经常存储一个指向关联数据结构的指针,有时也存储一个整数,它可能是其他数组的索引。

您可以将它用作对象被访问次数的计数器,或者其他什么。

唯一的缺点是,如果您的程序使用大量内存并且有数百万个对象,则每个标记的 4 个字节加起来,尤其是在您不使用它的情况下。在这种情况下,对于最多产的对象类型,您可能希望创建自己的不带标签的版本。

It's great! A freebie. I use it all the time to store one additional piece of information associated with the object.

Often I store a pointer to an associated data structure, or sometimes an integer which may be an index into some other array.

You can use it as a counter for times the object is accessed, or whatever.

The only downside is if your program uses lots of memory and you have millions of objects, those 4 bytes for each tag add up, especially if you're not using it. In that case, for your most prolific object type, you may want to create your own version without the tag.

吲‖鸣 2024-08-17 19:25:20

您的表单上有 2 个按钮,其中一个按钮设置为 Tag = 1,另一个按钮设置为 Tag = 2。现在,您将相同 OnClick 事件分配给这两个按钮,并编写如下代码:

procedure TForm28.Button1Click(Sender: TObject);
begin
  case (Sender as TButton).Tag of
   1: Caption := 'you pressed button 1';
   2: Caption := 'you pressed button 2';
  end;
end;

或更紧凑:

procedure TForm28.Button1Click(Sender: TObject);
begin
  Caption := 'you pressed button ' + IntToStr((Sender as TButton).Tag);
end;

基本上,标签将让您识别哪个控件触发了事件。想象一下,如果您有一个带有动态创建按钮的表单...一个包含数据库中的用户的列表,并且在每条记录上放置一个按钮“删除用户”。在这种情况下,您无法为每个按钮创建一个事件,您将创建一个分配给所有按钮的事件...并且您可以将用户 ID 放入标签中。这样,当您实现事件来处理所有按钮时,您就会知道要删除哪个用户。

You have 2 buttons on your form, on one you set the Tag = 1, and the other one Tag = 2. Now you assign the same OnClick event to both buttons and writhe the code like this:

procedure TForm28.Button1Click(Sender: TObject);
begin
  case (Sender as TButton).Tag of
   1: Caption := 'you pressed button 1';
   2: Caption := 'you pressed button 2';
  end;
end;

or more compact:

procedure TForm28.Button1Click(Sender: TObject);
begin
  Caption := 'you pressed button ' + IntToStr((Sender as TButton).Tag);
end;

Basically,Tag will let you identify what control fired the event. Think if you have a form with dynamically created buttons... a list with users from the database, and on each record you put a button "Delete User". In this situation you can't create an event for each button, you will create one event that will assigned to all the buttons... and you can put in the Tag the userid for example. That way when you implement the event to handle all the buttons, you'll know what user to delete.

情绪失控 2024-08-17 19:25:20

我一直使用标签。
这里有一些例子;

一个简单的示例:您有一个笔记本(就像没有选项卡的页面控件)
所以你可以将按钮定义为选项卡并编写

NoteBook.ActivePage := TButton(Sender).Tag;

更复杂的示例;
一个整数可以容纳 16 个按位布尔值;
然后,我可以检查发件人最多 16 个条件,以决定如何继续定价

If (BitCheck (Bit2,TButton(sender).tag=True) And BitCheck(bit12,TButton(Sender).Tag=False) Then
Begin
end;

If (BitCheck (Bit9,TButton(sender).tag=True) Or BitCheck(bit14,TButton(Sender).Tag=True) Then
Begin
end;

您明白了

I Use tags all the time.
here are some examples;

a simple sample: you have a notebook (Like a pagecontroll without tabs)
so you can define buttons as tabs and write

NoteBook.ActivePage := TButton(Sender).Tag;

A more complicated sample;
an integer can hold 16 bitwise bolleans;
I can then check the senders up to 16 conditions to decide how to continue the pricedure

If (BitCheck (Bit2,TButton(sender).tag=True) And BitCheck(bit12,TButton(Sender).Tag=False) Then
Begin
end;

If (BitCheck (Bit9,TButton(sender).tag=True) Or BitCheck(bit14,TButton(Sender).Tag=True) Then
Begin
end;

You Get the idea

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