对课堂上的每个项目重复此过程
Data.XX.NewValue := Data.XX.SavedValue;
Data.XX.OldValue := Data.XX.SavedValue;
我需要多次执行上述操作,其中 XX 代表类中的值。假设列表中有 3 项:Tim、Bob、Steve。有没有办法让三个人都执行上述操作,而无需将上述代码输入三次?
(Data是一个包含多个Object的类,每个Object类型为TList,其中包含OldValue、NewValue和SavedValue)
Data.XX.NewValue := Data.XX.SavedValue;
Data.XX.OldValue := Data.XX.SavedValue;
I need to do the above a large number of times, where XX represents the value in the class. Pretending there were 3 items in the list: Tim, Bob, Steve. Is there any way to do the above for all three people without typing out the above code three times?
(Data is a class containing a number of Objects, each type TList, which contain OldValue, NewValue and SavedValue)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我必须做这样的事情,我会做的就是在 Data 上再放一个 TList,其中包含其上所有对象的列表。将其填充到构造函数中,然后当您必须执行类似操作时,使用循环将相同的基本操作应用于列表中的每个项目。
What I'd do if I had to do something like this is put one more TList on Data, which holds a list of all the Objects on it. Fill it in the constructor, and then when you have to do something like this, use a loop to apply the same basic operation to each item in the list.
也许我不明白,好吧,但是......
这就是面向对象的闪光点。您为该类定义一个过程,然后应用于您创建的任何实例。
希望这有帮助
丹尼尔
Maybe I'm not understanding it ok but...
Here is where Object Oriented shines. You define a procedure for the class and then apply for any instance you create.
Hope this help
Daniel
从这篇文章和这篇文章,我建议如下:
现在你可以编写这种代码:
如果你需要更复杂的 代码如果要对数组进行操作,最好将其放入类中 - 替换仅在 TDataArray 类型的 efield 上编写的字段 - 并将操作数据的函数编写为此类的方法。
Judging from this post and this post, I would suggest the following :
Now you can write this kind of code :
If you need more complex manipulations on the array, it would be a good idea to put it into a class - replace the fields you wrote with only on efield of type TDataArray - and write the functions to manipulate the data as methods of this class.
我在这里会很小心。我知道使用通用接口和反射,或者其他一些更灵活、坦率地说写起来更有趣的自动化。避免这种诱惑。根据您的模式列出列表中的每一项并没有什么问题。模式是好的,代码将是可读的,易于执行,并且易于修改任何不适合该模式的单个属性。
避免输入所有内容的低技术方法是使用我们的老朋友 Excel。将所有属性放入 A 列,然后在 B 列中使用以下公式:
I would be careful here. I know the temptation is going to be to use a common interface and reflection, or some other automation that is more flexible and, frankly, more fun to write. Avoid this temptation. There is nothing wrong with listing every item in the list out according to your pattern. Patterns are good, and the code will be readable, easy to execute, and easy to modify any individual property that does not fit the pattern.
The low tech way to avoid typing everything out is to use our old friend Excel. Put all your properties in Column A, and then use this formula in column B: