Parameter Enums in C# -


i'm used programming in java project i'm supposed using c#, i'm trying convert packet system on java project, i'm running issues using c# compiler. here's code.

abstract class packet {     public static enum packettypes     {         invalid(-1), login(00);          private int packetid;          private packettypes(int packetid)         {             this.packetid = packetid;         }          public int getid()  { return packetid; }      } } 

this how it's done in java code, , have individual packets extend packet class. i'm trying figure out how make come in c#. perhaps having separate class each packet isn't way should done here?

unlike java enums classes, in c# enums plain values. cannot have member functions or fields.

one approach define extension method enum, this:

public static class packettypesextensions {     static readonly idictionary<packettypes,int> idfortype = new dictionary<packettypes,int> {         { packettypes.invalid, -1 }     ,   { packettypes.login,    0 }     };     static readonly idictionary<packettypes,string> descrfortype = new dictionary<packettypes,string> {         { packettypes.invalid, "<invalid packet type>" }     ,   { packettypes.login,   "<user login>" }     };     public static string description(this packettypes t) {         return descrfortype[t];     }     public static int id(this packettypes t) {         return idfortype[t];     } } 

this lets keep java syntax:

packettypes pt = ... // <<== assign packet type here int id = pt.id();    // calls static extension method string d = pt.description(); 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -