如何迭代与Delphi 6初始化的枚举类型,并避免“越界”错误? [英] How to iterate initialized enumerated types with Delphi 6 and avoid the "out of bounds" error?

查看:198
本文介绍了如何迭代与Delphi 6初始化的枚举类型,并避免“越界”错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Delphi 6 Professional。我正在与一个声明enumberated类型的DLL libraty接口,如下所示:

$ p $ $ $ $ $ $ TExtDllEnum =(ENUM1 = $ 0,ENUM2 = $ 1, ENUM3 = $ 2,ENUM4 = $ 4,ENUM5 = $ 8,ENUM6 = $ 10);

正如您所看到的,初始化的值不是连续的。如果我尝试使用for循环迭代类型,如下所示:

  var 
e:TExtDllEnum;
开始
为e:=低(TExtToDllEnum)为高(TExtToDllEnum)做
... //更多代码
结束;

每个循环调用,Delphi依然递增e,从而为e创建不是成员的数值枚举类型(例如3),并导致越界错误。我怎样才能迭代枚举类型的for循环,只生成枚举类型的有效值?



谢谢。


  type $ b $ 

b TExtDllEnum =(ENUM1 = $ 0,ENUM2 = $ 1,ENUM3 = $ 2,ENUM4 = $ 4,ENUM5 = $ 8,ENUM6 = $ 10);

const
CExtDllEnumSet = [ENUM1,ENUM2,ENUM3,ENUM4,ENUM5,ENUM6];


var
e:TExtDllEnum;
begin
e:= Low(TExtDllEnum);
而e <= High(TExtDllEnum)do
begin
如果e在CExtDllEnumSet中,则
WriteLn(Ord(e));

Inc(e);
end;

ReadLn;
结束。






并作为迭代器实现 - 只是为了好玩。
$ b $ $ $ $

$ b $ TExtDllEnum =(ENUM1 = $ 0,ENUM2 = $ 1,ENUM3 = $ 2,ENUM4 = $ 4, ENUM5 = $ 8,ENUM6 = $ 10);
const
CExtDllEnumSet = [ENUM1,ENUM2,ENUM3,ENUM4,ENUM5,ENUM6];

类型
TMyIterator =类别
私人
FValue:TExtDllEnum;
public
构造函数Create;
函数Next:TExtDllEnum;
函数HasNext:Boolean;
end;

构造函数TMyIterator.Create;
begin
FValue:= Low(TExtDllEnum);
end;

函数TMyIterator.HasNext:Boolean;
begin
结果:= FValue <=高(TExtDllEnum);
end;

函数TMyIterator.Next:TExtDllEnum;
begin
结果:= FValue;

重复
Inc(FValue);
直到(CExtDllEnumSet中的FValue)或(FValue> High(TExtDllEnum))
end;

var
MyIterator:TMyIterator;
begin
MyIterator:= TMyIterator.Create;

而MyIterator.HasNext做
WriteLn(Ord(MyIterator.Next));

MyIterator.Free;

ReadLn;
结束。


I am using Delphi 6 Professional. I am interfacing with a DLL libraty that declares an enumberated type as follows:

TExtDllEnum = (ENUM1 = $0, ENUM2 = $1, ENUM3 = $2, ENUM4 = $4, ENUM5 = $8, ENUM6 = $10);

As you can see the initialized values are not contiguous. If I try to iterate the type using a for loop as follows:

var
    e: TExtDllEnum;
begin
    for e := Low(TExtToDllEnum) to High(TExtToDllEnum) do
    ... // More code
end;

Delphi still increments e by 1 each loop invocation and thereby creates numeric values for e that are not members of the enumerated type (for example, '3'), and resulting in an 'out of bounds' error. How can I iterate the enumerated type in a for loop that generates only valid values for the enumerated type?

Thanks.

解决方案

By defining a set of constants...

type
  TExtDllEnum = (ENUM1 = $0, ENUM2 = $1, ENUM3 = $2, ENUM4 = $4, ENUM5 = $8, ENUM6 = $10);

const
  CExtDllEnumSet = [ENUM1, ENUM2, ENUM3, ENUM4, ENUM5, ENUM6];


var
  e: TExtDllEnum;
begin
  e := Low(TExtDllEnum);
  while e <= High(TExtDllEnum) do
  begin
    if e in CExtDllEnumSet then 
      WriteLn(Ord(e));

    Inc(e);
  end;

  ReadLn;
end.


and implemented as an iterator - just for fun...

type
  TExtDllEnum = (ENUM1 = $0, ENUM2 = $1, ENUM3 = $2, ENUM4 = $4, ENUM5 = $8, ENUM6 = $10);
const
  CExtDllEnumSet = [ENUM1, ENUM2, ENUM3, ENUM4, ENUM5, ENUM6];

type
  TMyIterator = class
  private
    FValue: TExtDllEnum;
  public
    constructor Create;
    function Next: TExtDllEnum;
    function HasNext: Boolean;
  end;

  constructor TMyIterator.Create;
  begin
    FValue := Low(TExtDllEnum);
  end;

  function TMyIterator.HasNext: Boolean;
  begin
    Result := FValue <= High(TExtDllEnum);
  end;

  function TMyIterator.Next: TExtDllEnum;
  begin
    Result := FValue;

    repeat
      Inc(FValue);
    until (FValue in CExtDllEnumSet) or (FValue > High(TExtDllEnum))
  end;

var
  MyIterator: TMyIterator;
begin
  MyIterator := TMyIterator.Create;

  while MyIterator.HasNext do
    WriteLn(Ord(MyIterator.Next));

  MyIterator.Free;

  ReadLn;
end.

这篇关于如何迭代与Delphi 6初始化的枚举类型,并避免“越界”错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆