2016-11-18 100 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