I have some code that has about 100+ constants that represent error codes
errorSyntax = 1
errorXYZ = 2
etc.. etc..
Thru out the code it says things like
if <somecondition> then errorCode=errorSyntax
....
...
if errorCode>0 then msgbox ErrorMessage(errorCode)
and elsewhere there is a huge SELECT case
select case errorCode
case errorSytax
s="Syntax Error"
case errorXYZ
s="An XYZ error has occured"
case else
s="Unknown error #"+str(errorCode)
end if
return s
All the above is just to illustrate what is happening now… and adding new error codes requires defining a new constant (and making sure it doesn’t overlap a previous one), then updating the SELECT/CASE statement
Can someone illustrate to me how this can be simplied (and I’m sure it can) using enumerations?
for an enum you dont have to assign values - enums get their “value” from their relative position in the enumerated values list
You CAN assign values if you want - you just dont HAVE to
The code doesn’t change much
For this
if <somecondition> then errorCode=errorSyntax
....
...
if errorCode>0 then msgbox ErrorMessage(errorCode)
error code gets defined as whatever you name the Enum and it can take on any legal value
The param passed gets retyped to the enum
For this code
select case errorCode
case errorSytax
s="Syntax Error"
case errorXYZ
s="An XYZ error has occured"
case else
s="Unknown error #"+str(errorCode)
end if
return s
and unfortunately the easiest way to do this is probably to create the enum, save as a text project, and edit it in a text editor to add all the enum values
simply haven’t got to a really old feature request to make it possible to paste in a list of values & create an enum from that list
Ok. Thanks… guess there really isn’t a whole lot of benefit (at least in this situation) .
Converting the constants to an Enum would be fairly simple
But then having to go thru 1000’s of lines of code and pre-pendng the Enum name to all the places “errorcode=” is would be cumersome (sure I know search/replace)
But the otherthing is the current set of constants have two ranges 101-999 are warnings, and 1000-9999 are errors, but since enums don’t have a discernable “value” that logic would become a bit (perhaps not much)more complex.
This makes warning1 = 0, warning2 = 1, error1 = 1000, error2 = 1001
So you can change the range easily
Say you need to make errors start at 5000
You change error1 = 1000 to error1 = 5000 and ALL other errors are renumbered subsequent to that
Nice & lazy