C# Use int constants vs Enums without cast -
i have int constants grouped in several enum, like:
enum myenum{ object_a = 10, object_b = 13 };
now have several functions take enums , something, like:
void myfunc(myenum e) { .... }
in code, need use myfunc in both these 2 ways:
myfunc(myenum.object_a); myfunc(13);
this gives error because ints aren't casted enum implicitly.
what recommend best practice preserving readability of code?
an obvious solution use myfunc((myenum)13); boring because 1 needs cast int every time , code gets heavy.
so far did (avoiding enums @ all):
using echannelint = system.int32; public static class echannel { public static readonly int ch_connection = 10; public static readonly int ch_data = 50; } public void doit(echannelint ch) { .... } doit(echannel.ch_data); doit(10);
which works, don't because seems "trick" or renaming thigs. suggest? perhaps "implicit operator" may useful?
thank you
you can overload myfunc
:
void myfunc(int i) { myfunc((myenum)i); }
Comments
Post a Comment