From else to else statement
- I would like to know something, how to jump from one if statement to another else. This is my code bellow:
private void buttonShrani_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(textBox1.Text) || comboBox1.SelectedItem == null || comboBox2.SelectedItem == null || comboBox3.SelectedItem == null) { if (comboBox2.SelectedItem != null) { if (comboBox2.GetItemText(comboBox2.SelectedItem) == "SOME MY TEXT") { if (String.IsNullOrEmpty(textBox1.Text) || comboBox1.SelectedItem == null || comboBox2.SelectedItem == null) { MessageBox.Show("Error message"); return; } else { //I WOULD LIKE TO JUMP FROM HERE.... } } } MessageBox.Show("Error message"); return; } else { // ... TO HERE }
If I use return; it goes out of the code. But I would like to go to the last "else" statement in this example.The thing is that IU have 3 comboBoxes and 1 textBox. In those 3 comboBoxes I have 2 options each.- In first case I use all 3 comboBoxes (the 1st if statement I declare that if one of them is emply it show up the MessegeBox)- In another I use only 2 comboBoxes, one HAS TO be emply. How to declare in the code that it would look at it?I did the code above, but I dont know how to get out of that else statement to the next else, that the code would jump over the MessegeBox.I know I complecated this, but anyway I would ask for some help.thx
Answers
- How about looking at it from a different angle:
Int32 invalidWidgets = 0; if(String.IsNullOrEmpty(textBox1.Text)) invalidWidgets++; if(null == comboBox1.SelectedItem) invalidWidgets++; if(null == comboBox2.SelectedItem) invalidWidgets++; if(null == comboBox3.SelectedItem) invalidWidgets++; switch (invalidWidgets) { case 0: // all items have data break; case 1: // one and only one item is missing data break; default: MessageBox.Show("Error message"); break; }- Proposed As Answer byRudedog2ModeratorTuesday, November 03, 2009 9:05 PM
- Marked As Answer byMitja Bonca Tuesday, November 03, 2009 10:45 PM
All Replies
- Nope. The goto must jump to a label that is in scope.
Just call a method. Or have both pieces of code goto a common third place.
In your specific case, you have a return in your "if" clause, so you don't need an else statement, just jump out.
private void buttonShrani_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(textBoxŠtevilka.Text) || comboBoxPomembnost.SelectedItem == null || comboBoxVrsta.SelectedItem == null || comboBoxPodročje.SelectedItem == null) { if (comboBoxVrsta.SelectedItem != null) { if (comboBoxVrsta.GetItemText(comboBoxVrsta.SelectedItem) == "Obvestilo") { if (String.IsNullOrEmpty(textBoxŠtevilka.Text) || comboBoxPomembnost.SelectedItem == null || comboBoxVrsta.SelectedItem == null) { MessageBox.Show("Niste izbrali vseh dodatnih možnosti, prosim da to storite.", "Pomanjkanje podatkov"); return; } else { //I WOULD LIKE TO JUMP FROM HERE.... goto myElse; } } } MessageBox.Show("Niste izbrali vseh dodatnih možnosti, prosim da to storite.", "Pomanjkanje podatkov"); return; } // ... TO HERE myElse: - GOTO statements arent usually recommended. They make the code harder to understand.Though i didnt fully understand your requirement, I modified the code so it goes to the second else. try and see if it suits for you.
private void buttonShrani_Click(object sender, EventArgs e) { bool _flag = false; if (String.IsNullOrEmpty(textBox1.Text) || comboBox1.SelectedItem == null || comboBox2.SelectedItem == null || comboBox3.SelectedItem == null) { if (comboBox2.SelectedItem != null) { if (comboBox2.GetItemText(comboBox2.SelectedItem) == "SOME MY TEXT") { if (String.IsNullOrEmpty(textBox1.Text) || comboBox1.SelectedItem == null || comboBox2.SelectedItem == null) { MessageBox.Show("Error message"); return; } else { //I WOULD LIKE TO JUMP FROM HERE.... _flag = true } } } MessageBox.Show("Error message"); return; } if(_flag==true && (any other condition u want to check) { //This is the second else block. // ... TO HERE }
Ganesh Ranganathan
[Please mark the post as answer if it answers your question]
blog.ganeshzone.net - How about looking at it from a different angle:
Int32 invalidWidgets = 0; if(String.IsNullOrEmpty(textBox1.Text)) invalidWidgets++; if(null == comboBox1.SelectedItem) invalidWidgets++; if(null == comboBox2.SelectedItem) invalidWidgets++; if(null == comboBox3.SelectedItem) invalidWidgets++; switch (invalidWidgets) { case 0: // all items have data break; case 1: // one and only one item is missing data break; default: MessageBox.Show("Error message"); break; }- Proposed As Answer byRudedog2ModeratorTuesday, November 03, 2009 9:05 PM
- Marked As Answer byMitja Bonca Tuesday, November 03, 2009 10:45 PM
Firstly, code to check for all the controls:
code to check for all the controls individually:if (String.IsNullOrEmpty(textBox1.Text) || comboBox1.SelectedItem == null || comboBox2.SelectedItem == null || comboBox3.SelectedItem == null) { //If either textBox1 (or) comboBox1 (or) comboBox2 (or) comboBox3 are empty, throw error msg and exit MessageBox.Show("Error message"); return; }
if (comboBox1.SelectedItem != null || comboBox2.SelectedItem == null || comboBox3.SelectedItem == null) { // //Do comboBox1 processing here // if (!String.IsNullOrEmpty(textBox1.Text)) { //if textBox1 contains text, do some processing } else { //if textBox1 doesn't contains text, do some processing } } if (comboBox1.SelectedItem == null || comboBox2.SelectedItem != null || comboBox3.SelectedItem == null) { // //Do comboBox2 processing here // if (!String.IsNullOrEmpty(textBox1.Text)) { //if textBox1 contains text, do some processing } else { //if textBox1 doesn't contains text, do some processing } } if (comboBox1.SelectedItem == null || comboBox2.SelectedItem == null || comboBox3.SelectedItem != null) { // //Do comboBox3 processing here // if (!String.IsNullOrEmpty(textBox1.Text)) { //if textBox1 contains text, do some processing } else { //if textBox1 doesn't contains text, do some processing } }A goto statement is used to transfer the program control directly to a labelled statement and is mostly used to get out of nested loops. Refer msdn article: http://msdn.microsoft.com/en-us/library/13940fs2(VS.71).aspx
I also remember reading an article back sometime which says, using goto is not a good programming practice. http://www.ifi.uzh.ch/req/courses/kvse/uebungen/Dijkstra_Goto.pdf
Balaji Baskar
I'd look at rewriting this series of if statements. In the code I quoted, you check if comboBo2.SelectedItem not null, then inside that if check if it is null. It will never be null.if (comboBox2.SelectedItem != null) { if (comboBox2.GetItemText(comboBox2.SelectedItem) == "SOME MY TEXT") { if (String.IsNullOrEmpty(textBox1.Text) || comboBox1.SelectedItem == null || comboBox2.SelectedItem == null)
Ron Whittle - If the post is helpful or answers your question, please mark it as such. Not As Brightly LitI would like to know something, how to jump from one if statement to another else. This is my code bellow:
private void buttonShrani_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(textBox1.Text) || comboBox1.SelectedItem == null || comboBox2.SelectedItem == null || comboBox3.SelectedItem == null) { if (comboBox2.SelectedItem != null) { if (comboBox2.GetItemText(comboBox2.SelectedItem) == "SOME MY TEXT") { if (String.IsNullOrEmpty(textBox1.Text) || comboBox1.SelectedItem == null || comboBox2.SelectedItem == null) { MessageBox.Show("Error message"); return; } else { //I WOULD LIKE TO JUMP FROM HERE.... } } } MessageBox.Show("Error message"); return; } else { // ... TO HERE }
If I use return; it goes out of the code. But I would like to go to the last "else" statement in this example.The thing is that IU have 3 comboBoxes and 1 textBox. In those 3 comboBoxes I have 2 options each.- In first case I use all 3 comboBoxes (the 1st if statement I declare that if one of them is emply it show up the MessegeBox)- In another I use only 2 comboBoxes, one HAS TO be emply. How to declare in the code that it would look at it?I did the code above, but I dont know how to get out of that else statement to the next else, that the code would jump over the MessegeBox.I know I complecated this, but anyway I would ask for some help.thx
You already understand that you over-complicated that. The code is an absolute mess. The logic needs to be cleaned and use the return properly. The nested if's are the source of the problem. GOTO only adds fire to the gunpowder.
//Check for error
if (String.IsNullOrEmpty(textBox1.Text) || comboBox1.SelectedItem == null || comboBox2.SelectedItem == null || comboBox3.SelectedItem == null)
{
MessageBox.Show("One or more required boxes have not been completed");//Error
return -1;//Optionally return a value indicating an error in the method.
}
if(Another thing is wrong)
MessageBox.Show("Error message");
//else...do your work now that there are no errors.
And keep going, what exists there is far too confusing to fix without clear requirements.
Good coding involves knowing one's logical limits and expanding them as necessary.How about looking at it from a different angle:
Int32 invalidWidgets = 0;
if(String.IsNullOrEmpty(textBox1.Text)) invalidWidgets++;
if(null == comboBox1.SelectedItem) invalidWidgets++;
if(null == comboBox2.SelectedItem) invalidWidgets++;
if(null == comboBox3.SelectedItem) invalidWidgets++;
switch (invalidWidgets)
{
case 0:
// all items have data
break;
case 1:
// one and only one item is missing data
break;
default:
MessageBox.Show("Error message");
break;
}
That is so much easier to read at a glance , than the nested if blocks with multiple exit points.
The nested IF blocks take time. Knowing there is only one exit point actually saves you time.
Mark the best replies as answers. "Fooling computers since 1971."How about looking at it from a different angle:
WOW... what a briliant solution! I will always use this one for a similar cases from now on!Int32 invalidWidgets = 0; if(String.IsNullOrEmpty(textBox1.Text)) invalidWidgets++; if(null == comboBox1.SelectedItem) invalidWidgets++; if(null == comboBox2.SelectedItem) invalidWidgets++; if(null == comboBox3.SelectedItem) invalidWidgets++; switch (invalidWidgets) { case 0: // all items have data break; case 1: // one and only one item is missing data break; default: MessageBox.Show("Error message"); break; }Many thanks mate!And thanks too all of you, a lot of useful answers! Thanks to all ones again.Thread asnwered!


