Else if statment

Here is some of my code, I get 2 errors, how would i fix it

[code]#pragma warning disable
if (lcarrier.Text == "Altell") ;
{
endadd.Text == "@message.alltel.com";
}
else if (lcarrier.Text == "AT&T");
{
endadd.Text == ("@mmode.com");
}[/code]


My 2 errors are ......

_
Invalid term else and ; expected


Answer this question

Else if statment

  • _CN_

    well this is what i've been told and what many of my colleagues use...I prefer to use Equals() for string

  • Ross B.

    You shouldn't use ToLower() and Equals(), you should use String.Compare(str1, str2, true)


  • Shaun Moore

    >> if (lcarrier.Text == "Altell") ; //What it is doing Here

    The syntax for an if() statement is:

    if (condition) statement ;

    this is normally written as

    if (condition)
    statement ;

    just to make it easier to read. Then to group many statement into one, they are wrapped in braces:

    if (condition)
    {
    statement ;
    statement ;
    statement ;
    }

    which is why these's no semi-colon at the end of the if() line.



  • eldiener

    does the same thing pretty much

  • fbalas

    ok...thanks

  • David d48701

    ok but re-read -....i was trying to make it all same case so its not case sensitive....

  • Andreas Asterlund

    i guess u should remove ; after both if and else if statements.


  • Derek Sanderson

    >> when comparing text, always use the Equals() method, not the operator =

    The is absolutely no need to prefer Equals over operator=. String.operator= merely calls String.Equals. Your statement is true for reference types which do not implement value semantic (string does). It is also is true for all classes in Java (but we're talk about C# here)



  • daijun

    Hello Remove ';' from if's and else's

    if (lcarrier.Text == "Altell") ; //What it is doing Here
    {
    endadd.Text == "@message.alltel.com";
    }
    else if (lcarrier.Text == "AT&T") ; // Again
    {
    endadd.Text == ("@mmode.com");
    }[/code]

    Remove them and I hope it'll be perfect now .

    Cheers



  • Chris Howarth

    when comparing text, always use the Equals() method, not the operator =

     

    overall your code should be :

    if (lcarrier.Text.ToLower().Trim().Equals("altell"))
    {
                    endadd.Text = "@message.alltel.com";
    }
    else if (lcarrier.Text.ToLower().Trim().Equals("at&t"))
    {
                    endadd.Text = "@mmode.com";
    }

     

    the text is trimmed (removes spaces) and is lowered to be 1 lower case and make sure its all equal and consistant



  • emepvsd

    ahmedilyas wrote:
    does the same thing pretty much

    Yes it does the same thing but not with the performance offered by String.Compare(). Write a test to compare 20000 strings from both methods, you'll see the difference.

    Cheers ;-)



  • hayek85

    Equals is not preferable, Always prefer String.Compare whiel comparing 2 strings, Its fast because it is evaluated on low level numeric values rather that == or Equals()... Its recommended my .Net Performance Articles on String usage.

    Best Regards,



  • Bibek

    ahmedilyas wrote:
    ok but re-read -....i was trying to make it all same case so its not case sensitive....


    Indeed - by lowercasing every string! The last parameter of String.Compare() being "true" tells it to ignore case. Much MUCH faster than lowercasing and returning a new copy of each string.

  • pfontyn

    Here is some page about this violation of ToLower and Compare methods:
    http://msdnwiki.microsoft.com/en-us/mtpswiki/c7d0f62f-32a1-4d63-a835-c0a398377436.aspx

    Problem is not about Equals() because this method should be used. But should be used only when we know the case of string characters. If because we don't know the case of string, we need to do some ToLower or ToUpper operation and then have Equal or == opeation, then is prefered Compare method. Maybe It's easyer to alway use Compare.



  • Else if statment