Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple conditions in switch C#

Tags:

c#

Unable to add multiple conditions in switch statement. How to do this using a switch statement? do i have to use multiple if conditions?

string oldValue = ".....long string1....";
string newValue = ".....long string2....";
switch (oldValue.Length && newValue.Length)
{
    case <500 && <500:
        //insert as is
        break;
    case >500 && <500:
        //split oldValue into array of strings and insert each
        //insert newValue as is
        break;
    case <500 && >500:
        //insert oldValue As is
        //split newValue into array of strings and insert each
        break;
    case >500 && >500:
        //split oldValue into array of strings and insert each
        //split newValue into array of strings and insert each
        break;
    default:
        break;
}
like image 478
Srikanth Avatar asked Apr 11 '26 08:04

Srikanth


2 Answers

No, you can't do this. switch statements test against values, not expressions.

You need to use if statements instead.

like image 102
GazTheDestroyer Avatar answered Apr 12 '26 20:04

GazTheDestroyer


Looking at your comments, the length of oldValue only affects what you do with oldValue, and the length of newValue only affects what you do with newValue.

Why not split this into two separate statements? Or even into a common method?

string[] GetValuesToInsert(String input)
{
    if (input.Length < 500)
        return new[] {input};
    else
        return input.Split(...);
}

whatever.Insert(GetValuesToInsert(oldValue));
whatever.Insert(GetValuesToInsert(newValue));
like image 40
Rawling Avatar answered Apr 12 '26 21:04

Rawling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!