Enums for non-Long types

acovitch
08-16-2005, 11:30 AM
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"
End Enum

Public Function foo()
Dim orientation As printerOrientation
orientation = ...
End Function


Any ideas?

Diurnal
08-16-2005, 11:53 AM
If you set up your enumeration and declare it in your routines then intellisense will pick them up for the programer:

Option Explicit

'Set up the enumeration.
Private Enum ePrinterOrientation
Portrait
Landscape
End Enum

Private Sub PrintJob(Orientation As ePrinterOrientation)

'Check the orientation.
If Orientation = Landscape Then
'blah.
ElseIf Orientation = Portrait Then
'blah.
Else
'Catch a problem here.
End If

End Sub

JDYoder
08-16-2005, 11:53 AM
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
Case Portrait
PrinterOrientationEnumToString = "Portrait"
Case Landscape
PrinterOrientationEnumToString = "Landscape"
End Select
End Function


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. :(

pranab
08-16-2005, 11:58 AM
How about storing the strings in a 2D array and creating the enum in such a way that array index and enum values matches ?

Diurnal
08-16-2005, 12: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.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum