c# - Method call based on Bool without if statements -
Preparation of a title was difficult, however, I would quickly explain my current options and hopefully someone gave me this Can tell a neater method to work
The first "quick" solution was we.
public square test {use public hair = some wrong; Use of Public Bunds Group Esee = false; Public Bull Usage Other = false; Public Zero MyMethod (if (UseSomething) {if (UseSomethingElse) {if (UseAnother) {// UseSomething, UseSomethingElse, UseAnother} else {// UseSomething, UseSomethingElse}} else {// UseSomething}} else if (UseSomethingElse) {If (UseAnother) {// UseSomethingElse, UseAnother} else {// UseSomethingElse}} // etc ...}}
Now this was an ugly solution in my opinion And really got destroyed quickly, especially if you wanted to add options
So I quickly came up with another solution as follows:
Public class Test {use public boole Something = false; Use of Public Bunds Group Esee = false; Public Bull Usage Other = false; Short Options = 0; Public Zero Init () // This is the @ Start Call {if (UseSomething) option + = 1 of your program; If (UseSomethingElse) option + = 2; If (UseAnother) option + = 4; } Public Zero MyMethod () {Some things = matching options (foo); } Public zero matching option (Fu Foo) {switch (option) // On the basis of option value (which is unique to each of the bull-trigger), follow a specific method. {Case 0: // 000 returns notepations (FU); Case 1: // 100 Return Options Softening (AF); Case 2: // 010 Return Options Group (FU); Case 4: // 001 Return ... etc; Case 3: // 110 break; Case 5: // 101 Break; Case 6: // 011 break; Case 7: // 111 break; Case -1: Return; }}}
Now, it makes more manageable and people who basically do to worry about, if / to put stuff in the rest of the statement Will not have to be. Apart from this, the methods are clean and
But still I can not let it go that there should be no other way of doing this.
This is not a lot of questions where the code will not work. It is high that I want a "best" or "most clean" way to do this. ^ _ ^ I am a third of software engineer students and still looking for ways to clean or adapt my code.
Please tell me if you have any comments or suggestions!
Note: This is mostly pseudo-code, I have not run or tested it. It was not about working, it is a concept that I am trying to explain.
I have to go and one more thing to do it for the way
First there is DoSomething hierarchy
abstract class BaseDoingThings {summarize zero do}. } Class something: BaseDoingThings {two (override) {...}} class SomethingElse: BaseDoingThings} {Do () {... override}
Then there is a test class < P> This is the basic idea that you have to adapt to your case, which means that you have to define the class test {personal list & amp; BaseDouting & gt; StuffToDo = new list & lt; Basdivingings & gt; (); Public Zero AddStuffToDo (BaseDoingThings TODO) {stuffToDo.Add (TODO); } Public Zero Execute () {foreach (var stuff in stuffToDo) {stuff.Do (); }}}
BaseDoingThings
interface correctly.
Comments
Post a Comment