locked
Use Enum in a Module in different class RRS feed

  • Question

  • I'm using Enum for the first time and of cource I'm a bit confused so I ask for help.

    I have an Enum in a Module like the following

    Module ModEnumTag
        Private Enum TagList
            AN   '- accetta caratteri stampabili
            AA   '- accetta solo lettere
            DB   '- nome per database
            NIP  '- accetta solo numeri interi positivi
            NI   '- accetta numeri interi positivi e negativi
            NDP  '- accetta numeri decimali solo positivi
            ND   '- accetta numeri decimali 
        End Enum
    End Module

    Now in a WinForm I have a Datagridview and I set  a Tag for each column in the DataGridView.

    Before going ahead with the application I wish to control if every Tag is one of the value in the Enum . How to do that considering that the Enum is in a Module ?

    This is the loop I wish to use

    Private Sub TagControl()
    
            For Each col As DataGridViewColumn In DGV.Columns
                Dim coltagvalue As String = col.Tag.ToString
    
    ''' check if ColTagValue is in the Enum 
    ''' ho to do ?
    
            Next
    
        End Sub

    Thanks for help

    Claudio


    Sunday, November 11, 2018 1:08 PM

Answers

  • Hello,

    Hope below code helps you, It will check whether value is present in ENUM or Not

    Change your ENUM from Private to Public 

    Public Enum TagList
    Dim isTheValueInTheEnum As Boolean = System.[Enum].IsDefined(GetType(ModEnumTag.TagList), coltagvalue)
                If isTheValueInTheEnum = True Then
                    'Present in ENUM
                Else '
                    'Not Present in ENUM
                End If

    • Marked as answer by Claudio111 Sunday, November 11, 2018 2:14 PM
    • Unmarked as answer by Claudio111 Sunday, November 11, 2018 2:39 PM
    • Marked as answer by Claudio111 Monday, November 12, 2018 4:45 PM
    Sunday, November 11, 2018 1:56 PM

All replies

  • Hello,

    Hope below code helps you, It will check whether value is present in ENUM or Not

    Change your ENUM from Private to Public 

    Public Enum TagList
    Dim isTheValueInTheEnum As Boolean = System.[Enum].IsDefined(GetType(ModEnumTag.TagList), coltagvalue)
                If isTheValueInTheEnum = True Then
                    'Present in ENUM
                Else '
                    'Not Present in ENUM
                End If

    • Marked as answer by Claudio111 Sunday, November 11, 2018 2:14 PM
    • Unmarked as answer by Claudio111 Sunday, November 11, 2018 2:39 PM
    • Marked as answer by Claudio111 Monday, November 12, 2018 4:45 PM
    Sunday, November 11, 2018 1:56 PM
  • It gives me an error here

    System.[Enum].IsDefined(GetType(ModEnumTag.TagList), coltagvalue)

    'ModEnumTag' is not accessible in this context because it is "Friend"

     
    Sunday, November 11, 2018 2:10 PM
  • sorry,   solved.

    Thank You  ...Aspirant ;)

    Good Luck

    Sunday, November 11, 2018 2:14 PM
  • May I ask You one more question about Enum 
    Sunday, November 11, 2018 2:39 PM
  • Yes please ask . Why you unmarked the answer any issues with same ?
    Sunday, November 11, 2018 3:54 PM
  • I solved all the problems with Enum

    I unmarked your answer to say you that I needed more help , but now is ok

    Thank you again

    Monday, November 12, 2018 4:45 PM