Results 1 to 8 of 8

Thread: Enumerations Theory

  1. #1

    Thread Starter
    Hyperactive Member Sal's Avatar
    Join Date
    Mar 2000
    Posts
    262

    Enumerations Theory

    Hi,

    Can someone please explain what an Enumeration is and how it works, along with why you use it? This is relating to my use of the RichEdit control provided by VBAccelerator.com. I am trying to use the common dialog control to load a file without using advanced API calls.

    Thanks,
    Sal

    VB Code:
    1. Public Enum ERECFileTypes
    2.     SF_TEXT=1
    3.     SF_RTF=2
    4. End Enum
    5.  
    6. Private Function LoadDoc(ByVal sFile As String) As Boolean
    7. Dim eType As ERECFileTypes
    8.  
    9.    If sFile = "" Then
    10.       If Not VBGetOpenFileName(sFile, , , , , , "RichText Documents (*.RTF)|*.RTF|Text Documents (*.TXT)|*.TXT|All Files (*.*)|*.*", 1, , "Choose File To Open", "RTF", Me.hWnd) Then
    11.          Exit Function
    12.       End If
    13.    End If
    14.    
    15.    If pbDetectFileType(sFile, eType) Then
    16.       If edtMain.LoadFromFile(sFile, eType) Then
    17.          edtMain.Modified = False
    18.          m_sFileName = sFile
    19.          LoadDoc = True
    20.       End If
    21.    End If
    22.    
    23. End Function
    VB-6 Professional Edition (Sp3)
    www.safemall.cc
    [email protected]

    Climb & Maintain FL410,
    Cleared Direct Rainr

  2. #2
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Does MSDN helps?

    Enum Statement


    Declares a type for an enumeration.

    Syntax

    [Public | Private] Enum name

    membername [= constantexpression]

    membername [= constantexpression]

    . . .

    End Enum

    The Enum statement has these parts:

    Part Description
    Public Optional. Specifies that the Enum type is visible throughout theproject. Enum types are Public by default.
    Private Optional. Specifies that the Enum type is visible only within themodule in which it appears.
    name Required. The name of the Enum type. The name must be a valid Visual Basic identifier and is specified as the type when declaringvariables orparameters of the Enum type.
    membername Required. A valid Visual Basic identifier specifying the name by which a constituent element of the Enum type will be known.
    constantexpression Optional. Value of the element (evaluates to a Long). If no constantexpression is specified, the value assigned is either zero (if it is the first membername), or 1 greater than the value of the immediately preceding membername.


    Remarks

    Enumeration variables are variables declared with an Enum type. Both variables and parameters can be declared with an Enum type. The elements of the Enum type are initialized to constant values within the Enum statement. The assigned values can't be modified atrun time and can include both positive and negative numbers. For example:

    Enum SecurityLevel
    IllegalEntry = -1
    SecurityLevel1 = 0
    SecurityLevel2 = 1
    End Enum

    An Enum statement can appear only atmodule level. Once the Enum type is defined, it can be used to declare variables, parameters, orprocedures returning its type. You can't qualify an Enum type name with a module name. Public Enum types in aclass module are not members of the class; however, they are written to thetype library. Enum types defined instandard modules aren’t written to type libraries. Public Enum types of the same name can't be defined in both standard modules and class modules, since they share the same name space. When two Enum types in different type libraries have the same name, but different elements, a reference to a variable of the type depends on which type library has higher priority in the References.

    You can't use an Enum type as the target in a With block.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  3. #3
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    I use it, for example, when I want to define the parameters a function or sub would be waiting for. For example, I could write this code:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Enum Numbers
    4.     Zero = "000"
    5.     One = "001"
    6.     Two = "010"
    7.     Three = "011"
    8.     Four = "100"
    9.     Five = "101"
    10.     Six = "110"
    11.     Seven = "111"
    12. End Enum
    13.  
    14. Private Sub Form_Load()
    15.     PopUp Five
    16.     PopUp Six
    17.     PopUp Seven
    18. End Sub
    19.  
    20. Private Sub PopUp(Text As Numbers)
    21.     MsgBox Text
    22. End Sub

    This is a stupid example... but if the enum and Popup sub (in this example) are in a DLL, you could use it wherever you'd like and would know exactly what the Sub is waiting (using the IntelliSense, obviously) Besides, you could use mnemonics ("Six", for example) to represent something more difficult to remember ("110", in this case -Wow... that's hard to remember -)
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  4. #4
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Actually... with that scope you could only use it in the module it's written.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  5. #5
    Megatron
    Guest
    Basically, it's a way of organizing constants. For example.
    VB Code:
    1. Private Enum ECOLOR
    2.     LIGHT_YELLOW = &H00C0FFFF&
    3.     LIGHT_PURPLE = &H00FFC0C0&
    4.     LIME_GREEN = &H0000FF00&
    5.     BLACK = 0
    6. End Enum
    7.  
    8. Private Sub ChangeColor(ByVal Clr As ECOLOR)
    9.     Form1.BackColor = Clr
    10. End Sub

    Conclusion
    Constants make it possible for you to forget all of those ugly numbers, and to just remember the word that represents it.

    Enumerations are simply a group of constants.

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Another cool thing is you can also use the enumerated elements like regular constants too. Like in Megatron's example:
    VB Code:
    1. Private Enum ECOLOR
    2.     LIGHT_YELLOW = &H00C0FFFF&
    3.     LIGHT_PURPLE = &H00FFC0C0&
    4.     LIME_GREEN = &H0000FF00&
    5.     BLACK = 0
    6. End Enum
    7.  
    8. Private Sub ChangeColor(ByVal Clr As ECOLOR)
    9.     Form1.BackColor = Clr
    10. End Sub
    11.  
    12.  
    13. 'you can also use them like this
    14.  
    15. If Form1.BackColor=BLACK then Form1.Forecolor=LIGHT_YELLOW

  7. #7

    Thread Starter
    Hyperactive Member Sal's Avatar
    Join Date
    Mar 2000
    Posts
    262
    Thanks folks,

    This information has proven to be invaluable in my conceptualising the practical uses of enumerations.

    Sal
    VB-6 Professional Edition (Sp3)
    www.safemall.cc
    [email protected]

    Climb & Maintain FL410,
    Cleared Direct Rainr

  8. #8

    Thread Starter
    Hyperactive Member Sal's Avatar
    Join Date
    Mar 2000
    Posts
    262
    ...It seems as though this is an Array on steroids, and designed for functions.

    Sal
    VB-6 Professional Edition (Sp3)
    www.safemall.cc
    [email protected]

    Climb & Maintain FL410,
    Cleared Direct Rainr

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width