Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 344
  • Last Modified:

Help with C# code

I'm getting an error in this snippet:

            if (e.KeyChar == 13)
               
                if (txtManualDescription.Text.Length() == '0')
                    txtManualquantity.Focus();
           
            {
                if (txtManualDescription.Text.Substring(0, 2) == "RX")
                    txtManualDescription.Text = "*" + txtManualDescription.Text;
                    txtManualquantity.Focus();
            }
            else if(e.KeyChar == 27)
            {
                exitManual();
            }
        }

The error says invalid expression term else and it wants a semi-colon at the end of the else if statement.

Any help would be greatly appreciated!!

Thanks,
Jerry
0
JDL129
Asked:
JDL129
  • 3
1 Solution
 
databoksCommented:
Post the Method/Class. Also use the Code snippet and let us know which line where the problem is.
0
 
databoksCommented:
if (e.KeyChar == 13)
               
                if (txtManualDescription.Text.Length() == '0')
                    txtManualquantity.Focus();
           
            {
                if (txtManualDescription.Text.Substring(0, 2) == "RX")
                    txtManualDescription.Text = "*" + txtManualDescription.Text;
                    txtManualquantity.Focus();
            }
            else if(e.KeyChar == 27)
            {
                exitManual();
            }
        }

You need a "{" in after the If in the top like this:


     if (e.KeyChar == 13)
                {
                if (txtManualDescription.Text.Length() == '0')
                    txtManualquantity.Focus();
           
            {
                if (txtManualDescription.Text.Substring(0, 2) == "RX")
                    txtManualDescription.Text = "*" + txtManualDescription.Text;
                    txtManualquantity.Focus();
            }
            else if(e.KeyChar == 27)
            {
                exitManual();
            }
        }
0
 
Julian HansenCommented:
The code is ambiguous because you have a statement block enclosed in { } which is legal but makes no sense because it is not linked to an if / for / while etc.

if (e.KeyChar == 13) // You probably need a { here to create the if block for the matching else below
  if (txtManualDescription.Text.Length() == '0')
    txtManualquantity.Focus(); // this is a complete statement so the { on the next line makes no sense and probably belongs one line up
           
    {
      if (txtManualDescription.Text.Substring(0, 2) == "RX") // you have two statements following an if with no enclosing { } block - the indentation suggests the statements belong together so one would assume that this line needs an opening {
        txtManualDescription.Text = "*" + txtManualDescription.Text;
        txtManualquantity.Focus();
    }
    else if(e.KeyChar == 27) // This is most likely matched to the if at the top
    {
      exitManual();
    }
  }

Open in new window


Fixing it will depend on the context of what you are trying to do so this might be one valid way of doing it

if (e.KeyChar == 13)
{
  if (txtManualDescription.Text.Length() == '0') txtManualquantity.Focus();
  if (txtManualDescription.Text.Substring(0, 2) == "RX")  {
    txtManualDescription.Text = "*" + txtManualDescription.Text;
    txtManualquantity.Focus();
  }
}
else if(e.KeyChar == 27) {
  exitManual();
  }
}

Open in new window

0
 
databoksCommented:
Always remember if you have more than one line of code in a If statement you always have to use Curly Braces.

For example this won't work
string Username = Console.ReadLine();
if(Username.Equals("John"))
Console.WriteLine("You are John. Im glad it's You");
int age = 21;
else
Console.WriteLine("Your name is not John. Im sad);
int age = -21;

Open in new window


but this will compile:
string Username = Console.ReadLine();
if(Username.Equals("John"))
{
Console.WriteLine("You are John. Im glad it's You");
int age = 21;
}
else
{
Console.WriteLine("Your name is not John. Im sad);
int age = -21;
}

Open in new window

0
 
JDL129Author Commented:
Julian H!!!!!!!!!!!!!!!1

Thanks for the help!!!

Jerry
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now