Solved

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

Posted on 2008-06-17
7
203 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

744 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

10 Experts available now in Live!

Get 1:1 Help Now