473,943 Members | 3,585 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exiting a Subroutine

Greetings,

I'm still relatively new to C# and was wondering if there was a way to
exit a subroutine (such as "Exit Sub" in VB)? I have the code below
which performs validations. If the user fails to enter in a First name,
then I want to set the focus to the control on the form and then exit
the subroutine. Any ideas?

private void btnSubmit_Click (object sender, System.EventArg s e)
{
...

if (this.txtFirstN ame.Text == "")
{
MessageBox.Show ("First Name is required");
this.txtFirstNa me.Focus();
// I then want to exit the subroutine.
}
if (Lastname == "")
{
...

Thanks in advance!

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
7 7638
Hey,

You can use the return keyword for this:

if (this.txtFirstN ame.Text == "")
{
MessageBox.Show ("First Name is required");
this.txtFirstNa me.Focus();
return;
}

Hope that helps,
Clint

OutdoorGuy wrote:
Greetings,

I'm still relatively new to C# and was wondering if there was a way to exit a subroutine (such as "Exit Sub" in VB)? I have the code below
which performs validations. If the user fails to enter in a First name, then I want to set the focus to the control on the form and then exit
the subroutine. Any ideas?

private void btnSubmit_Click (object sender, System.EventArg s e)
{
..

if (this.txtFirstN ame.Text == "")
{
MessageBox.Show ("First Name is required");
this.txtFirstNa me.Focus();
// I then want to exit the subroutine.
}
if (Lastname == "")
{
..

Thanks in advance!

*** Sent via Developersdex http://www.developersdex.com ***


Nov 17 '05 #2
Just use "return":

private void btnSubmit_Click (object sender, System.EventArg s e)
{
...
if (this.txtFirstN ame.Text == "")
{
MessageBox.Show ("First Name is required");
this.txtFirstNa me.Focus();
// I then want to exit the subroutine.
return; <-------
}

Nov 17 '05 #3
Just use "return". "Return" can also be used in VB.Net.

--
Tim Wilson
..Net Compact Framework MVP

"OutdoorGuy " <Ou********@fis hing.com> wrote in message
news:uX******** ******@TK2MSFTN GP12.phx.gbl...
Greetings,

I'm still relatively new to C# and was wondering if there was a way to
exit a subroutine (such as "Exit Sub" in VB)? I have the code below
which performs validations. If the user fails to enter in a First name,
then I want to set the focus to the control on the form and then exit
the subroutine. Any ideas?

private void btnSubmit_Click (object sender, System.EventArg s e)
{
..

if (this.txtFirstN ame.Text == "")
{
MessageBox.Show ("First Name is required");
this.txtFirstNa me.Focus();
// I then want to exit the subroutine.
}
if (Lastname == "")
{
..

Thanks in advance!

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #4
yes, just call,

return ""; or whatever you want. The return call will cause the function to
exit at that point...

glenn
"OutdoorGuy " <Ou********@fis hing.com> wrote in message
news:uX******** ******@TK2MSFTN GP12.phx.gbl...
Greetings,

I'm still relatively new to C# and was wondering if there was a way to
exit a subroutine (such as "Exit Sub" in VB)? I have the code below
which performs validations. If the user fails to enter in a First name,
then I want to set the focus to the control on the form and then exit
the subroutine. Any ideas?

private void btnSubmit_Click (object sender, System.EventArg s e)
{
..

if (this.txtFirstN ame.Text == "")
{
MessageBox.Show ("First Name is required");
this.txtFirstNa me.Focus();
// I then want to exit the subroutine.
}
if (Lastname == "")
{
..

Thanks in advance!

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #5
Thanks, Clint. That seems to do the trick. However, my "Focus()"
statement is not working. I am using a tab control, so could this be
the issue? The control I want to set the focus to (i.e.,
"txtFirstna me") is on the first "tab page". Do I first need to navigate
to the tab page and then set the focus to the appropriate control?

Thanks again!

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #6

OutdoorGuy wrote:
Thanks, Clint. That seems to do the trick. However, my "Focus()"
statement is not working. I am using a tab control, so could this be
the issue? The control I want to set the focus to (i.e.,
"txtFirstna me") is on the first "tab page". Do I first need to navigate to the tab page and then set the focus to the appropriate control?

Thanks again!

*** Sent via Developersdex http://www.developersdex.com ***


I believe you do have to set the correct tab first, then call Focus().
Out of curiousity, did this work correctly before the "return;" was put
in?

Nov 17 '05 #7
Thanks for the info, Clint. I simply had to use the "Show" method of
the tab page object and then use the "Focus" method to set the focus to
the appropriate control. (See sample code below).

In answer to your question, the "Focus" method didn't work before I put
in the "Return" statement either. The key was simply selecting the tab
page first.

if (this.txtLastNa me.Text == "")
{
this.tabPage1.S how();
MessageBox.Show ("'Last Name' is required");
this.txtLastNam e.Focus();
return;
}

Thanks again!

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4979
by: Michael Farber | last post by:
Not sure if this is the right group for this but anyways... I've got an ASP web application that uses a Visual Basic component to do some work. I instantiate the component in asp and then attempt to call the subroutine. The subroutine is actually invoked maybe 2 out of 10 times run. What could possibly be the reason for this?? Why the inconsistency in running the subroutine? Cheers
4
1746
by: SHPsalm139 | last post by:
If I'm in, say, a 3rd level sub and want to exit, not only the sub but the entire procedure, without a GoTo, can this be done without using switches?
2
1434
by: singlal | last post by:
Hi, my question was not getting any attention because it moved to 2nd page; so posting it again. Sorry for any inconvenience but I need to get it resolved fast. Need your help! **************************************************************************************************** Original Question: -------------------- Has anyone called a COBOL subroutine using COBOL CALL from a COBOL/DB2
3
1692
by: IamIan | last post by:
I am using os.spawnv in Python 2.1 to do some geoprocessing in a subroutine/process. Everything works great, except when the processing is done the subroutine just waits for a couple minutes before closing itself and returning to the main script. I have tried using sys.exit() and exit() but these aren't doing anything. What is the proper way to terminate this subroutine upon completion, rather than waiting? Thank you.
10
1902
by: nasau | last post by:
Perl, I have a main program which calls two subroutines (depending upon the report names).In the subroutine I am printing the data from CSV file using the format variable, Format_top prints the report header and STDOUT prints the report data. Both these subroutine runs fine individually, independent of each other. Now I have included both these subroutine in my main program and I call these subroutine depending upon the name of the report. Here...
1
1916
by: peterv6 | last post by:
I'm using a "package" type subroutine, called test_package.pl. I'm calling it from a script called split0.pl. I want to pass the $0 variable, use the subroutine to split out just the filename, and pass that filename value back to the calling script. The subroutine does the processing correctly (I've verified with print statements), but I'm having trouble getting the value passed back to the calling script. Here's the subroutine: package...
4
2625
by: otterbyte | last post by:
Hi, I have a bit of code which is confusing me to no end. Here are the basics: 1) The class module is being used in the module of a form. 2) There is an instance of the object declared at the form level, and instantiated in the Load event of the form. This instance holds the "current" values (based on data in a table), and it's called ordCurrent.
3
13644
by: sangith | last post by:
Hi, I have question on processing the file handle in a subroutine. Here is my program without subroutine: open FH1, "<outfile" or die "cannot open the file for reading: $!\n"; while ($line_from_outfile = <FH1>) { chomp $line_from_outfile; print $line_from_outfile;
3
1457
shrek123
by: shrek123 | last post by:
How can I pass output of some perl subroutine to a subroutine? I have Subroutine1 and wanna pass the return value of this subroutine as an argument to another subroutine. I tried this; &SubroutineA("test",&SubroutineB) but it doest work. Thanks in advance.
0
10135
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9970
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
11299
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6087
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6308
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4910
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.