30

i know that in vb.net you can just do Exit Sub

but i would like to know how do i exit a click event in a button?

here's my code:

private void button1_Click(object sender, EventArgs e) { if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "") { //exit this event } } 
0

7 Answers 7

45

Use the return statement.

MSDN Reference

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

Comments

26

Use the return keyword.

From MSDN:

The return statement terminates execution of the method in which it appears and returns control to the calling method. It can also return the value of the optional expression. If the method is of the type void, the return statement can be omitted.

So in your case, the usage would be:

private void button1_Click(object sender, EventArgs e) { if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "") { return; //exit this event } } 

Comments

12
return; // Prematurely return from the method (same keword works in VB, by the way) 

Comments

3

I'd suggest trying to avoid using return/exit if you don't have to. Some people will devoutly tell you to NEVER do it, but sometimes it just makes sense. However if you can structure you checks so that you don't have to enter into them, I think it makes it easier for people to follow your code later.

5 Comments

Disagree with you in so many ways. The reason some people think like that is because they write functions that are way too long.
The two extremes are "check for what you don't want & return with early exit" or "check for what you want, and nest as deep as is needed" (which can be a LOT). Please don't encourage the latter!
I agree. In the example listed though, rather than check for empty and have to use return inside, I'd suggest checking for conditions that don't require the use of a return later.
Returning in the middle of a function is perfectly acceptable. @Thyamine: What exactly do you mean by "checking for conditions that don't require the use of a return late"?
@KevinAnderson Hear Hear!! I'm currently working on a Page_Load that uses the latter alternative. It starts with "if the employee name is found", runs through about 850 lines of nested if blocks, and then has "else send an employee not found message". Messy.
3

Use the return keyword.

return; //exit this event 

Comments

2

Yo can simply google for "exit sub in c#".

Also why would you check every text box if it is empty. You can place requiredfieldvalidator for these text boxes if this is an asp.net app and check if(Page.IsValid)

Or another solution is to get not of these conditions:

private void button1_Click(object sender, EventArgs e) { if (!(textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")) { //do events } } 

And better use String.IsNullOrEmpty:

private void button1_Click(object sender, EventArgs e) { if (!(String.IsNullOrEmpty(textBox1.Text) || String.IsNullOrEmpty(textBox2.Text) || String.IsNullOrEmpty(textBox3.Text))) { //do events } } 

1 Comment

Guess which site and question is now the top hit on Google.
2

There are two ways to exit a method early (without quitting the program):

i) Use the return keyword.
ii) Throw an exception.

Exceptions should only be used for exceptional circumstances - when the method cannot continue and it cannot return a reasonable value that would make sense to the caller. Usually though you should just return when you are done.

If your method returns void then you can write return without a value:

return; 

2 Comments

would anyone like to comment on whether throwing an exception is good or not?
The comment about exceptions made by the answerer is a good way to say it. Exceptions are "heavy" on performance, and don't necessarily add useful information if your program should know how to react. So a typical plan: to communicate validation or other expected problems - such as in the OP - it can work better to use a pre-defined return code such as an int or other flag. Have good code comments about the values :). An exception can then be used for "anything else" that happens after your "normal" problem checks and return codes...your last "else".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.