2016-11-18 12 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; 

ループは次のようになります。

if I = Voting_System.Option(None) then -- ... 
のように、私はすでに私の心に来たすべてのものを試してみました

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'Val("None") then -- ... 

とそれ以外のバージョンでは動作しませんでした。

これを達成するためのアイデアはもうありません。あなたの質問では、この行に基づいて

+2

'I'の型は' Votes_Distribution'のインデックス型です。 'Party'は' Option'ではありません。あなたがしていることは意味をなさないと思われます。 'Party'が' None'を含む 'Option'のサブタイプとして他の場所で宣言された場合、' if 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 othersタイプUniversal_Integerの作業に移行したため、すべてのケースで対応しました。

関連する問題