在delphi中传递不同枚举类型的混合 [英] Pass a mixture of differend enums types in delphi
本文介绍了在delphi中传递不同枚举类型的混合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
键入
TEnumOne =(eOneFlagOne, eOneFlagTwo);
TEnumTwo =(eTwoFlagOne,eTwoFlagTwo);
该方法应该采用不同的枚举:
进程([eOneFlagOne,eTwoFlagTwo]);
我正在尝试这样执行:
//不起作用
procedure Process(const枚举:Variant数组);
var aValue;变量
开始
为枚举中的一个值
开始
//当然不能工作...
如果aValue是TEnumOne然后
结束;
结束
那么,是否有一种类型而不是Variant,我可以选择吗?或者是不同的方法,我不会看到
解决方案
满足pascal的美丽。
以下是您可能想要做的工作示例:
程序Project34; {$ APPTYPE CONSOLE}
type
TEnum =(eOneFlagOne,eOneFlagTwo,eTwoFlagOne,eTwoFlagTwo);
TEnumSet = set of TEnum;
const
cEnumOne = [eOneFlagOne,eOneFlagTwo];
cEnumTwo = [eTwoFlagOne,eTwoFlagTwo];
程序进程(const枚举:TEnumSEt);
var e:TEnum;
开始
在Enums中$ e
WriteLn(ord(e));
结束
begin
进程([eOneFlagOne,eTwoFlagTwo]);
进程(cEnumOne);
进程(cEnumTwo);
结束。
请注意,您也可以声明这样的常量。也许这更清楚了:
const
cEnumOne:TEnumSet = [eOneFlagOne,eOneFlagTwo];
cEnumTwo:TEnumSet = [eTwoFlagOne,eTwoFlagTwo];
I need to write a procedure where can pass different enum selections.
type
TEnumOne = (eOneFlagOne, eOneFlagTwo);
TEnumTwo = (eTwoFlagOne, eTwoFlagTwo);
The method should take different enums:
Process([eOneFlagOne, eTwoFlagTwo]);
I'm trying to implement it like this:
// does not work!
procedure Process(const Enums: array of Variant);
var aValue; Variant
begin
for aValue in Enums do
begin
// of course, can't work...
if aValue is TEnumOne then
end;
end;
So, is there a type instead of Variant, I can choose? Or is the a different approach, I dont' see?
解决方案
Meet the beauty of pascal.
Here's a working example of what you're probably trying to do:
program Project34; {$APPTYPE CONSOLE}
type
TEnum=(eOneFlagOne,eOneFlagTwo,eTwoFlagOne,eTwoFlagTwo);
TEnumSet=set of TEnum;
const
cEnumOne=[eOneFlagOne,eOneFlagTwo];
cEnumTwo=[eTwoFlagOne,eTwoFlagTwo];
procedure Process(const Enums: TEnumSEt);
var e:TEnum;
begin
for e in Enums do
WriteLn(ord(e));
end;
begin
Process([eOneFlagOne, eTwoFlagTwo]);
Process(cEnumOne);
Process(cEnumTwo);
end.
Note that you can also declare the constants like this. Maybe that's clearer:
const
cEnumOne:TEnumSet=[eOneFlagOne,eOneFlagTwo];
cEnumTwo:TEnumSet=[eTwoFlagOne,eTwoFlagTwo];
这篇关于在delphi中传递不同枚举类型的混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文