2016-11-18 153 views
0

我想遍历数组并检查当前数组索引是否为枚举值。该阵列以及枚举被定义如下:比较枚举值

type Option is (None, A, B, C, D); 
type Votes is array(Option) of Natural; 

Zero_Option_Distribution: constant Votes := (others => 0); 
Votes_Distribution: Votes := Zero_Option_Distribution; 

的循环如下所示:

for I in Voting_System.Votes_Distribution'Range loop 
    -- this is where I would like to check whether I is a representation of either of the enum values 
end loop; 

我已经尝试过,来到了我心中的一切,就像

if I = Voting_System.Option(None) then -- ... 

and

if I'Val("None") then -- ... 

和一些更多的版本,他们每个人都没有工作。

我真的没有更多的想法来实现这一点。基于这条线在你的问题

+2

那么'I'的类型是'Votes_Distribution'的索引类型,它是'Party'而不是'Option'。所以你在做什么似乎没有意义。如果'Party'在其他地方被声明为包含'None'的'Option'的子类型,那么'如果I = None'应该可以工作(假设您使相关声明可见)。 –

+2

用Party的定义更新您的问题可能会让问题变得清晰一些。 – NWS

回答

2

你比较枚举类型,就像任何其他类型的对象的对象的值,使用=

if I = None then 
    ... 
end if; 
+0

感谢您的回答,我在提问后20分钟就找到了解决方案。 – hGen

0
-- this is where I would like to check whether I is a representation of either of the enum values 

,我认为Party是Integer子什么的?您应该能够只使用这样的事情:

-- (Checks if I is 0) 
if (Integer(I) = Voting_System.Option'Pos(Voting_System.Option.None)) then 
-- ... 
0

如果是根据ARM95 3.5.1§7和ARM95 Annex K§175,你可以尝试类似这样的方式:

for I in Votes_Distribution'Range loop 
    case Party'Pos (I) is 
    when Option'Pos (None) => Put_Line (I'Img & " is for «None»"); 
    when Option'Pos (A) => Put_Line (I'Img & " is for «A»"); 
    when Option'Pos (B) => Put_Line (I'Img & " is for «B»"); 
    when Option'Pos (C) => Put_Line (I'Img & " is for «C»"); 
    when Option'Pos (D) => Put_Line (I'Img & " is for «D»"); 
    when others   => Put_Line ("I not in Option"); 
    end case; 
    end loop; 

Ne编辑其他在所有情况下,因为我们搬到类型Universal_Integer