pureCod
pureCod

Reputation: 13

C# Case which include other cases

How to execute in one case, other cases? I could just copy paste those other cases, or put it to some external function but there is so much code so I dont want to do that.

Example:

 switch(foo) 
 {     
    case 3:     
    {             
        //something     
    }break;          
    
    case 4:     
    {             
        //something else     
    }break;      
    
    case 5:     
    {             
        //here i want to execute case 3 and case 4     
    }break;      
}

I think that this was previously answered but I can't find how to do it.

Upvotes: 1

Views: 340

Answers (6)

Astrid E.
Astrid E.

Reputation: 2872

I would argue that the code being intuitive is important; hence, I would suggest defining helper variables that clarify intention.

While not knowing the meaning of 3, 4 and 5, a hypothetical example could be:

var awesomeFoos = new[] { 3, 5 };
var popularFoos = new[] { 4, 5 };

var fooIsAwesome = awesomeFoos.Contains(foo);
var fooIsPopular = popularFoos.Contains(foo);

if (fooIsAwesome)
{
    // something (preferably refactored to a separate method)
}

if (fooIsPopular)
{
    // something else (preferably refactored to a separate method)
}

, where .Contains() is found in the System.Linq namespace.

An example fiddle is found here.


That being said, though; you seem quite determined that you would prefer to keep your code as-is, to an as large extent as possible. If that is really a high priority, you could consider putting the whole foo-switch logic inside a method and let it call itself twice in the case 5 scenario:

private static void HandleFoo(int foo)
{
    switch(foo) 
    {     
       case 3:     
       {             
           // something
       }break;          

       case 4:     
       {             
           // something else
       }break;      

       case 5:     
       {             
           HandleFoo(3);
           HandleFoo(4);
       }break;      
    }
}

Example fiddle is found here.

(Depending on the content of // something and // something else, this may not be feasible, though.)

Upvotes: 1

Shervin Ivari
Shervin Ivari

Reputation: 2501

I strongly recommend changing the way you want to implement this statement. This method is not suitable for modern applications and is coupled with everything. But if you need to implement as you asked, You can jump between cases by using goto.

For more information Read "jump statements".

int a = 10;

switch (a)
{
case 0:
//Condition1:
//some actions
break;
case 1:
    goto case 0;
    //or
    goto Condition1;
    break;
default:
    break;
}

Since this is the linear approach you should check conditions in if for each goto in each case(cause you can't Go back to each step)

Another approach is to save all cases in the order you want to execute and run the switch multiple times. I use a while in my example you can use goto if you don't want to use a loop.

Queue<int> cases = new Queue<int>();
//1 is the main switch value
cases.Enqueue(1);
while (cases.Count > 0)
{
int temp = cases.Dequeue();
switch (temp)
{
    case 0:
        Console.WriteLine("0");
        break;
    case 1:
        Console.WriteLine("1");
        cases.Enqueue(3);//run case 3
        cases.Enqueue(0);//then run case 0
        break;
    case 2:
        Console.WriteLine("2");

        break;
    case 3:
        Console.WriteLine("3");
        break;
    default:
        break;
}
}

Upvotes: -1

Andrei Khotko
Andrei Khotko

Reputation: 247

C# doesn't have such functionality. You have to create other methods which will do actions for cases 3 and 4 and call them from case 5 branch. I would suggest to create a separate class FooHandler which would handle your value. It's easily extendable and readable.

public class FooHandler
{
    private readonly int _foo;

    public FooHandler(int foo)
    {
        this._foo = foo;
    }

    public void Handle()
    {
        switch(this._foo) 
        {     
            case 3: this.HandleCase3(); break;          
            case 4: this.HandleCase4(); break;
            case 5: this.HandleCase5(); break;
            default: throw new ArgumentException("Foo value is invalid");
        }
    }
    
    private void HandleCase3()
    {
        // Your code for case 3
    }

    private void HandleCase4()
    {
        // Your code for case 4
    }

    private void HandleCase5()
    {
        this.HandleCase3();
        this.HandleCase4();
    }
}

Usage:

var fooHandler = new FooHandler(foo);
fooHandler.Handle();

Upvotes: 3

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112279

It would be easier to use if-statements. Here I also used pattern matching to simplify the tests.

if (foo is 3 or 5) {
    // something
}
if (foo is 4 or 5) {
    // something else
}

So simple and easy to read and understand.

Upvotes: 1

Klaus G&#252;tter
Klaus G&#252;tter

Reputation: 11977

You could also change the switch to two ifs:

if (foo == 3 || foo == 5) 
{
    //something
} 
if  (foo == 4 || foo == 5) 
{ 
    //something else
}

Upvotes: 1

wohlstad
wohlstad

Reputation: 28074

If you don't want to add methods (you didn't explain why),
you can use Action local variables holding Lambda expressions.

In the example below you can replace the body of the lambdas with whatever code you have for "something" and "something else".
Action also supports passing arguments to the lambda's body if you need them.

Action something =      () => { Console.WriteLine("something"); };
Action something_else = () => { Console.WriteLine("something_else"); };

switch (foo)
{
    case 3:
        something();
        break;

    case 4:
        something_else();
        break;

    case 5:
        something();
        something_else();
        break;
}

Upvotes: 1

Related Questions