08-16-2005, 12:30 PM
Is there a way to set up an enumeration for value types other than Long? I would like to use Strings.
For example, a printing application might have an orientation variable whose only valid values would be Portrait and Landscape. I would like to offer only those options to the programmer. I realize that the following code does not actually work, because Enums only take Longs, but this is what I am trying to do:
Private Enum printerOrientation
Portrait = "PORTRAIT"
Landscape = "LANDSCAPE"
Public Function foo()
Dim orientation As printerOrientation
orientation = ...
08-16-2005, 12:53 PM
If you set up your enumeration and declare it in your routines then intellisense will pick them up for the programer:
'Set up the enumeration.
Private Enum ePrinterOrientation
Private Sub PrintJob(Orientation As ePrinterOrientation)
'Check the orientation.
If Orientation = Landscape Then
ElseIf Orientation = Portrait Then
'Catch a problem here.
08-16-2005, 12:53 PM
I've wished many times to do this, but alas, VB6 doesn't allow for it. (Though you sort of can in VB.NET). Anyway, you can create two functions to translate back and forth for your coding purposes...
Function PrinterOrientationEnumToString(var as printerOrientation) as string
Select Case var
PrinterOrientationEnumToString = "Portrait"
PrinterOrientationEnumToString = "Landscape"
And another function to do the opposite, but as you can see, it's open to errors if misspellings are made, or it's not kept up-to-date, etc. It's a bummer, I know. :(
08-16-2005, 12:58 PM
How about storing the strings in a 2D array and creating the enum in such a way that array index and enum values matches ?
08-16-2005, 01:26 PM
For example, a printing application might have an orientation variable whose only valid values would be Portrait and Landscape. I would like to offer only those options to the programmer.Maybe I am not seeing your question correctly but that is what an enumeration will do. If you need to return the name of the long in the enumeration to display then you will need to write some kind of translation routine to return a string such as JDYoder provided above.