|
-
Jan 11th, 2002, 04:57 PM
#1
Thread Starter
Hyperactive Member
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:
Public Enum ERECFileTypes
SF_TEXT=1
SF_RTF=2
End Enum
Private Function LoadDoc(ByVal sFile As String) As Boolean
Dim eType As ERECFileTypes
If sFile = "" Then
If Not VBGetOpenFileName(sFile, , , , , , "RichText Documents (*.RTF)|*.RTF|Text Documents (*.TXT)|*.TXT|All Files (*.*)|*.*", 1, , "Choose File To Open", "RTF", Me.hWnd) Then
Exit Function
End If
End If
If pbDetectFileType(sFile, eType) Then
If edtMain.LoadFromFile(sFile, eType) Then
edtMain.Modified = False
m_sFileName = sFile
LoadDoc = True
End If
End If
End Function
-
Jan 11th, 2002, 05:37 PM
#2
Need-a-life Member
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.
-
Jan 11th, 2002, 05:44 PM
#3
Need-a-life Member
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:
Option Explicit
Private Enum Numbers
Zero = "000"
One = "001"
Two = "010"
Three = "011"
Four = "100"
Five = "101"
Six = "110"
Seven = "111"
End Enum
Private Sub Form_Load()
PopUp Five
PopUp Six
PopUp Seven
End Sub
Private Sub PopUp(Text As Numbers)
MsgBox Text
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.
-
Jan 11th, 2002, 05:46 PM
#4
-
Jan 11th, 2002, 05:48 PM
#5
Basically, it's a way of organizing constants. For example.
VB Code:
Private Enum ECOLOR
LIGHT_YELLOW = &H00C0FFFF&
LIGHT_PURPLE = &H00FFC0C0&
LIME_GREEN = &H0000FF00&
BLACK = 0
End Enum
Private Sub ChangeColor(ByVal Clr As ECOLOR)
Form1.BackColor = Clr
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.
-
Jan 11th, 2002, 06:06 PM
#6
Another cool thing is you can also use the enumerated elements like regular constants too. Like in Megatron's example:
VB Code:
Private Enum ECOLOR
LIGHT_YELLOW = &H00C0FFFF&
LIGHT_PURPLE = &H00FFC0C0&
LIME_GREEN = &H0000FF00&
BLACK = 0
End Enum
Private Sub ChangeColor(ByVal Clr As ECOLOR)
Form1.BackColor = Clr
End Sub
'you can also use them like this
If Form1.BackColor=BLACK then Form1.Forecolor=LIGHT_YELLOW
-
Jan 11th, 2002, 09:33 PM
#7
Thread Starter
Hyperactive Member
Thanks folks,
This information has proven to be invaluable in my conceptualising the practical uses of enumerations.
Sal
-
Jan 11th, 2002, 09:39 PM
#8
Thread Starter
Hyperactive Member
...It seems as though this is an Array on steroids, and designed for functions.
Sal
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|