Getting the max value of an enum

后端 未结 11 2291
旧时难觅i
旧时难觅i 2020-12-07 13:42

How do you get the max value of an enum?

11条回答
  •  失恋的感觉
    2020-12-07 14:13

    There are methods for getting information about enumerated types under System.Enum.

    So, in a VB.Net project in Visual Studio I can type "System.Enum." and the intellisense brings up all sorts of goodness.

    One method in particular is System.Enum.GetValues(), which returns an array of the enumerated values. Once you've got the array, you should be able to do whatever is appropriate for your particular circumstances.

    In my case, my enumerated values started at zero and skipped no numbers, so to get the max value for my enum I just need to know how many elements were in the array.

    VB.Net code snippets:

    '''''''
    
    Enum MattType
      zerothValue         = 0
      firstValue          = 1
      secondValue         = 2
      thirdValue          = 3
    End Enum
    
    '''''''
    
    Dim iMax      As Integer
    
    iMax = System.Enum.GetValues(GetType(MattType)).GetUpperBound(0)
    
    MessageBox.Show(iMax.ToString, "Max MattType Enum Value")
    
    '''''''
    

提交回复
热议问题