c# - This redundant Readonly prefix -


internal abstract class readonlycoefs {     public const boolean allowed = true;     public const boolean disallowed = false;      public abstract boolean getcoef(); }  internal class coefs : readonlycoefs {     public override boolean getcoef()  { ... }     public void setcoef()  { ... } } 

now, suppose want use somewhere this

if (variable == readonlycoefs.allowed)     ... 

i don't think want have readonly prefix.

in case can add instance methods isallowed , setallowed, setdisallowed, if there lot of consts, how in such case?

no, there no way want, @ least now. in c# 6, there ability import static members of class using directive:

using static somenamespace.readonlycoefs; 

that allow access static members of readonlycoefs without specifying type name.

that said, given current design agree hans should using enum:

enum coefpermission {     allowed,     disallowed, }  internal abstract class readonlycoefs {     public abstract coefpermission getcoef(); } 

then:

if (variable == coefpermission.allowed) ... 

that should compatible using static feature in c# 6. i.e. have using static somenamespace.coefpermission; , code read if (variable == allowed) ....

now, that said, i'm skeptical of current design. maybe makes sense non-bool values (especially if switch enum-based approach). didn't show that. you've got bool values, , imho making constants or enums silly. instead, why not have like:

internal abstract class readonlycoefs {     public abstract boolean iscoefallowed(); }  internal class coefs : readonlycoefs {     public override boolean iscoefallowed()  { ... }     public void setiscoefallowed()  { ... } } 

then:

if (variable) ... 

even better if give variable name:

if (iscoefallowed) ... 

imho, that's lot more readable , maintainable.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -