如何比较枚举?

时间:2015-06-24 23:26:25

标签: vba ms-access-2013

我在检查我的枚举是否相同时遇到了很多麻烦。

我的枚举声明如下:

Public Enum POKEMON_VERSION
    RED_BLUE_YELLOW = 1
    GOLD_SILVER = 2
    ...
End Enum

我的表格是这样的:

Const pokemonVersion As Variant = POKEMON_VERSION.GOLD_SILVER
Public Sub Search_ButtonClick()
    Dim sql As String
    sql = Get_Pokemon(pokemonVersion ) 'which is GOLD_SILVER
    Me.mySubform.Form.RecordSource = sql
    Me.mySubform.Requery
End Sub 

以下是我遇到问题的方法,我的开关声明:

  Public Function Get_Pokemon( version As Variant ) As String
     Select Case version
          Case version = RED_BLUE_YELLOW
              ' don't get ability or nature, didn't exist in this version
               Get_Pokemon = "SELECT * FROM ..." 
          Case version = GOLD_SILVER
               Get_Pokemon = "SELECT * FROM ..."
     End Select
  End Function

但是当我运行该程序时,它发现version在我的任何情况下都不匹配(在此示例中,应与GOLD_SILVER匹配)。我认为问题可能是我将VariantEnum""值"进行比较。

我该如何解决这个问题?

我不会介意改变Const pokemonVersion的类型,而是改变Enum""值"确实需要存储在Const变量中,这是我能想到的最好的。

1 个答案:

答案 0 :(得分:2)

问题与ConstEnum无关。它与您的Select语法有关。您正在基于version进行选择,因此案例需要是枚举的成员。当你这样做时(为了清楚起见,添加了括号)......

Case (version = RED_BLUE_YELLOW)

......它评估为......

Case (version = 1)

...因此,如果您为RED_BLUE_YELLOW传递version,则会得到......

Case (1 = 1)

......这是:

Case True
    '...
Case False
    '...
Case False
'etc.

您可以更改Select以启用True ...

Public Function Get_Pokemon(version As POKEMON_VERSION) As String
    Select Case True
        Case version = RED_BLUE_YELLOW
            ' don't get ability or nature, didn't exist in this version
             Get_Pokemon = "SELECT * FROM ..."
        Case version = GOLD_SILVER
             Get_Pokemon = "SELECT * FROM ..."
    End Select
End Function

...或者使用下面更清晰,更有效的语法:

Public Function Get_Pokemon(version As POKEMON_VERSION) As String
    Select Case version
        Case RED_BLUE_YELLOW
            ' don't get ability or nature, didn't exist in this version
             Get_Pokemon = "SELECT * FROM ..."
        Case GOLD_SILVER
             Get_Pokemon = "SELECT * FROM ..."
    End Select
End Function