C# - using a generic enum (or alternative method for controlling lists) -


i'm writing pathfinding algorithm game, trying keep generic can used in future applications.

i have node class holds x, y , "passabletype".

the nodegrid class stores array of nodes, containing graph information of how connect, , has findastarpath() function, takes parameters startnode, endnode, , params "passabletypes".

my problem determining type "passabletype" should have.

ideally want able use generic enum - i.e. restricted list each game defines. node hold single element of list, path type (the current game may use path, grass, wall, etc)

thus, when entity tries path, provides pathfinding function types treat "passable". man may use

findastarpath(currentnode, destinationnode, "path", "floor", "door"); 

but car may use

findastarpath(startnode, endnode, "road"); 

my problem can't work out how nodegrid take generic enum or equivalent logic.

at moment have taking strings, means have write
myenum.road.tostring() every time use it.

ideally i'd

nodegrid<myenum> currentnodegrid = new nodegrid<myenum>() 

and nodes take myenum passabletype, pathfinding functions, allowing each game have different set of tile types pathing.

but can't define nodegrid as:

public class nodegrid<t> t:enum 

for clarity, part of pathfinding function uses enum (contained within node):

    public bool ispassable(string[] passabletypes)     {         (var = 0; < passabletypes.count(); i++)         {             if (this.passabletype == passabletypes[i]) return true;         }         return false;     } 

thanks haighstrom

unless you're using specific functionality of enums (like enum.parse), don't see reason constrain them. freeing constraints, callers can use whatever types see fit, beit enum, or set of string values (as have it), or set of custom class instances check against.

public class nodegrid<t> {     public t passabletype { get; private set; }      public bool ispassable(params t[] passabletypes)     {         return ispassable((ienumerable<t>)passabletypes);     }      public bool ispassable(ienumerable<t> passabletypes)     {         foreach(t passtype in passabletypes)         {             if (equalitycomparer<t>.default.equals(this.passabletype, passtype))                 return true;         }          return false;     } } 

but since we're using generics, can't use == comparison anymore. simplest leverage equalitycomparer.default utility. main reason use on directly calling this.passabletype.equals(passtype) perform null checks , leverage generics applicable , if types implement iequatable<t>, use generic versions. other minor things. call object.equals overload.


some examples based on question:

//using custom enum, calls params t[] overload nodegrid<mycarenum> carnode = ... carnode.ispassable(mycarenum.road, mycarenum.tunnel);  //demonstrates receiving set of pass types strings external source list<string> passtypes = new list<string>("path", "floor", "door"); nodegrid<string> personnode = ... personnode.ispassable(passtypes) //calls ienumerable<t> overload  //feel free declare enums wherever want,  //it can avoid potential mixups this: nodegrid<string> airplanenode = ... nodegrid<string> personnode = ... nodegrid<mycarenum> carnode = ... airplanenode.ispassable("floor"); //makes no sense, compile personnode.ispassable("clouds"); //makes no sense, compile carnode.ispassable("sky"); //compile error: expected mycarenum value 

Comments