Solved

C# VS 2008 windows app HOW DO I RETURN FROM A VOID METHOD?

Posted on 2008-06-17
7
205 Views
Last Modified: 2013-12-16
C# VS 2008 windows app HOW DO I RETURN FROM A VOID METHOD?

In this small code snippet if I have the user click canel how do I return back to whrever in the program they were before this method. It is void so I cannot use return, break doesn't do it? Thoughts? Thanks!

public void btnFullSearch_Click(object sender, EventArgs e)
        {
            MessageBox.Show(" Continue?", "Test", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            RecursiveSearch.RecSearch(txtShare.Text, txtFile.Text);
        }
0
Comment
Question by:Karl66
7 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 21804033
you can just return;
0
 

Expert Comment

by:mjhoagland
ID: 21804221
Why would you need to return in such a small method anyway?

You show a message box and do a search.  This doesn't look like the place you need to do a return anyway.

A return in your RecSearch() method would be better placed.
0
 

Author Comment

by:Karl66
ID: 21804387
ok a different example - using a CASE I can use BREAK. Assuming I dont want if result == cancel goToThisMethod(); what can I use to just EXIT A METHOD? Thanks.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 18

Expert Comment

by:philipjonathan
ID: 21804393
Change the line

MessageBox.Show(" Continue?", "Test", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

to

if (MessageBox.Show(" Continue?", "Test", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel) return;
0
 
LVL 22

Expert Comment

by:p_davis
ID: 21804408
that should work
0
 
LVL 5

Accepted Solution

by:
Hendo1973 earned 500 total points
ID: 21804410

public void btnFullSearch_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(" Continue?", "Test", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
               RecursiveSearch.RecSearch(txtShare.Text, txtFile.Text);
        }

Open in new window

0
 
LVL 18

Expert Comment

by:philipjonathan
ID: 21804412
Karl66:
ok a different example - using a CASE I can use BREAK. Assuming I dont want if result == cancel goToThisMethod(); what can I use to just EXIT A METHOD? Thanks.

The answer is "return" :)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question