Solved

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

Posted on 2008-06-17
7
204 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel file not created as expected 7 51
Connection String 16 43
ASP.NET 5 Templates 2 68
C# MVC and a global static factory that never dies 4 49
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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.

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now