Reputation: 12104
Say I have a class called Enabler
I make an instance of the class like I'd normally do (in semi-pseudo c# code)
bool cond;
Enabler enable = new Enabler(cond);
the problem arises when I call the Enabler's method called Enable
, it's not so much of a problem as it is horribly verbose
enable.Enable(/*some code to enable*/);
the real problem arises when I do this:
Enabler disable = new Enabler(!cond); //reverse condition
disable.Enable(/*some code to disable*/); //this makes no sense semantically
so I'm looking for a way to do something like this:
Enabler enable = new Enabler(cond);
Enabler disable = new Enabler(!cond);
// then simply call the method by the object name like this:
enable(/*something to enable*/); //uses the object name as the method name
disable(/*something to disable*/);
this makes loads more sense semantically than saying disable.Enable
is there any way to do this at all?
just like it's possible to do a this[]
, is it also possible to make a this()
of sorts?
Upvotes: 1
Views: 119
Reputation: 1479
afaik it's not possible to do something like this. But if you can Disable and Enable with your "Enabler" class, "Enabler" probably is not the right name anyway.
What about "Toggler" or something more verbose?
Edit: @Nahum's answer is also a good hint. To have well maintainable code, you should change both imo.
Upvotes: 3
Reputation: 7197
just call the method Activate
or Execute
and have two instances
Enabler enable= new Enabler(cond);
Enabler disable = new Enabler(!cond);
enable.Activate()
disable.Activate()
Also I feel enabler is a bad class name.
Your class handles both Enabling and Disabling it is not clear from your class name what it does.
You should name it StatusChanger
or whatever is appropriate for your application.
Also why don't you just have two methods, enable
and disable
?
Upvotes: 2