Grouping Enums
-
i am sending messages over the network. i am doing that by first creating a message object and it has : -timecreated as date -priority as integer -sourcenode as enum (what part of the sender is creating the message) -targetnode as enum (wat part of the reciever has to solve the message) -sourcename as string (the sender name) -targetname as string (the reciever name , because multiple receivers could be on the same socket) -command as enum (to tell the part of the reciever what to do) -pvalue1 as object (custom extras) -pvalue2 as object (custom extras) -pvalue3 as object (custom extras) these messages are (de)serialized to : - string , to show them to my logscreens, and script them when needed in the future - stream , to send them over the network My Problem is with the command-enum , my list is becoming extremely big ! I am now in the progress of changing the enum into a class , with x number of classes in it, with x number of enums in it. I am doing this because it looks/feels nice when i type code , meaning : CmdE.General.Nada But now my questions start: -do i have to declare command as object , is there a trick to not have to ? -how do i check if command = cmde.general.niks ? -do i have any heavy changes to expect ?
Jarno Burger Video Jockey
-
i am sending messages over the network. i am doing that by first creating a message object and it has : -timecreated as date -priority as integer -sourcenode as enum (what part of the sender is creating the message) -targetnode as enum (wat part of the reciever has to solve the message) -sourcename as string (the sender name) -targetname as string (the reciever name , because multiple receivers could be on the same socket) -command as enum (to tell the part of the reciever what to do) -pvalue1 as object (custom extras) -pvalue2 as object (custom extras) -pvalue3 as object (custom extras) these messages are (de)serialized to : - string , to show them to my logscreens, and script them when needed in the future - stream , to send them over the network My Problem is with the command-enum , my list is becoming extremely big ! I am now in the progress of changing the enum into a class , with x number of classes in it, with x number of enums in it. I am doing this because it looks/feels nice when i type code , meaning : CmdE.General.Nada But now my questions start: -do i have to declare command as object , is there a trick to not have to ? -how do i check if command = cmde.general.niks ? -do i have any heavy changes to expect ?
Jarno Burger Video Jockey
Hi, I don't think you can achieve hierarchical enums. This is the closest I got (sorry, I am using C# here, I trust VB.NET supports similar things):
abstract class enumbase { public enum general { }; public enum special { }; } class cmdA : enumbase { public new enum general { een, twee, drie } public new enum special { een, twee, drie } } class cmdE : enumbase { public new enum general { niemendal1 } public new enum special { niemendal2 } } class test { enumbase.general my0=cmdE.general.niemendal1; enumbase.general my1=cmdA.general.een; enumbase.special my2=cmdA.special.twee; }
and now the problem is the "my" things have a specific type, there is no user-selectable base type for all the enums (enumbase is not OK since that is a class, whereas the "my" things are enum values, hence ints. One ugly way to overcome this is by using ints and casting, as in
int my0=(int)cmdE.general.niemendal1;
. If you do so, there is no need for the abstract class enumbase any more, since you can cast all unrelated enums to ints anyway. Having no way to do this ina clean way seems logical, you probably want all enum values to be different, the only way to enforce that in programming languages such as C/Java/C# is by putting them all inside a single large enum. And unfortunately you can't split them in multiple files, since the compiler won't let you add a "partial" keyword to an enum statement. So you are stuck to either one huge enum, or, if it fits the application, a non-OO approach, with some bit fiddling: assume cmd is an int with some bits indicating top-level command, next few bits indicate next-level subcommand, etc.int cmd=...;
int topLevelCommand=cmd & topLevelBitMask;
object command=topLevelDictionary[topLevelCommand];
int nextLevelCommand=cmd & nextLevelBitMask;
object subcommand=command.Dictionary[nextLevelCommand];
..etcMaybe this scheme suits your needs, maybe it does not. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
modified on Wednesday, October 29, 2008 9:37 PM
-
i am sending messages over the network. i am doing that by first creating a message object and it has : -timecreated as date -priority as integer -sourcenode as enum (what part of the sender is creating the message) -targetnode as enum (wat part of the reciever has to solve the message) -sourcename as string (the sender name) -targetname as string (the reciever name , because multiple receivers could be on the same socket) -command as enum (to tell the part of the reciever what to do) -pvalue1 as object (custom extras) -pvalue2 as object (custom extras) -pvalue3 as object (custom extras) these messages are (de)serialized to : - string , to show them to my logscreens, and script them when needed in the future - stream , to send them over the network My Problem is with the command-enum , my list is becoming extremely big ! I am now in the progress of changing the enum into a class , with x number of classes in it, with x number of enums in it. I am doing this because it looks/feels nice when i type code , meaning : CmdE.General.Nada But now my questions start: -do i have to declare command as object , is there a trick to not have to ? -how do i check if command = cmde.general.niks ? -do i have any heavy changes to expect ?
Jarno Burger Video Jockey
If you want the command to be an enum, you have to have all the values in the same enum. However, you can create static classes with properties to create an hierarchy for the values:
Enum AllCommands
General_Nada
Error_IO_FileNotFound
Error_IO_PathNotFound
Error_Access_Denied
End EnumPublic Shared Class Command
Public Shared Class General
Public ReadOnly Property Nada As AllCommands
Get
Return AllCommands.General_Nada
End Get
End Property
End Class
Public Shared Class Error
Public Shared Class IO
Public ReadOnly Property FileNotFound As AllCommands
Get
Return AllCommands.Error_IO_FileNotFound
End Get
End Property
Public ReadOnly Property PathNotFound As AllCommands
Get
Return AllCommands.Error_IO_PathNotFound
End Get
End Property
End Class
Public Shared Class Access
Public ReadOnly Property Denied As AllCommands
Get
Return AllCommands.Error_Access_Denied
End Get
End Property
End Class
End Class
End ClassNow you can get the FileNotFound command either by using
AllCommands.Error_IO_FileNotFound
orCommand.Error.IO.FileNotFound
.Despite everything, the person most likely to be fooling you next is yourself.