0

could I turn this into a switch statement ?

if (donation_euro.Text.Trim().Equals(""))
        {
            donation_euro.Text = "00.00";
        }
        if (donation_lui.Text.Trim().Equals(""))
        {
            donation_lui.Text = "00.00";
        }
        if (donation.Text.Trim().Equals(""))
        {
            donation.Text = "00.00";
        }
2
  • 1
    Why don't you associate the string with a float, and then parse the float as a string when necessary. e.g. float donationEuro = 0.0F, and then donation_euro.Text = ""+donationEuro. Commented Dec 1, 2013 at 15:38
  • i guess your intent is just to initialize the textboxes ? Commented Dec 1, 2013 at 15:47

2 Answers 2

8

No, because your are not switching on a single variable, but multiple.

I suspect your motivation to do this, is to make the code more readable ? If this is the case, you could put the common logic of your three if's into a method, to reuse the code and better convey the intent.

Sign up to request clarification or add additional context in comments.

Comments

2

Not Possible.as switch takes Expression and executes the matching Constant Case Label.

From MSDN :Switch-Case

Each case label specifies a constant value. The switch statement transfers control to
the switch section whose case label matches the value of the switch expression

Switch(Expression)
{

case constant1://statements
             break;
case constant2://statements
             break;
case constant3://statements
             break;
}

if you want to switch with single value then it is possible

    int a = 3;    
    if (a == 1)
    {
        //statements
    }
    else if(a == 2)
    {
        //statements
    }
    else if(a == 3)
    {
       //statements
    }
    else if(a == 4)
    {
       //statements
    }
    else
    {
       //statements
    }

can be converted into switch as below:

int a = 3; 
switch(a)
{
case 1: //statements
break;


case 2: //statements
break;


case 3: //statements
break;


case 4: //statements
break;


default : //statements
break;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.