return may be used in void methods, but may as well be omitted.
public void TestMethod()
{
MessageBox.Show("This is a void method that shows a message and returns nothing");
return; // may be omitted
}
// use:
TestMethod();// will show a message
ddanbe
Industrious Poster
4,277 posts since Oct 2008
Reputation Points: 2,121
Solved Threads: 720
Skill Endorsements: 26
return can also be used as flow control in methods, even void methods
public void TestMethod() {
if (someStatus == true) return;
DoOtherStuffHere();
}
You use this to remove decision making from the calling method and placing it within the called method. This is useful when the information you need to make the decision is hidden from the calling method as you don't need to add more visibility to things that don't need it (Information hiding).
Momerath
Senior Poster
3,726 posts since Aug 2010
Reputation Points: 1,322
Solved Threads: 624
Skill Endorsements: 12
return means you want to return "something" from your method.
for example:
you have a method called "addNumbers"
here it is:
private void addNumbers(int a, int b)
{
return a + b;
}
once you call the method by passing values:
addNumbers(9, 8);
it will "return" a value "back" to you!
if you know for a fact that you want to get a value from a method:
you use "methods that returns a value"
otherwise, use void method.
just like the example you gave.
MessageBox.Show("Whatever");
anthonyjpv
Junior Poster in Training
98 posts since Oct 2010
Reputation Points: 16
Solved Threads: 7
Skill Endorsements: 0
Not automatically, you'd have to tell the compiler where the Main method is found (it's automatically set when you create the project).
I use Visual Studio 2010, and this information is found at Project-><app name> Properties on the Application tab. It's the Startup Object dropdown.
It's possible that the system would find the Main method on its own, but this ensures that it finds the right one.
Momerath
Senior Poster
3,726 posts since Aug 2010
Reputation Points: 1,322
Solved Threads: 624
Skill Endorsements: 12
you can use the messagebebox.show to show your result
but even though you just called the method
behind the scenes it actually returns the value back
in the other hand you can output the method into controls such as textbox, etc
or you can put the value returned back into a variable for later processing
and for the static
sometimes you want to hide your methods from the user or to other programmer
so if you do, you dont use static method
they must know the class name in order to call your method
hope this helps!
anthonyjpv
Junior Poster in Training
98 posts since Oct 2010
Reputation Points: 16
Solved Threads: 7
Skill Endorsements: 0
Hey dude
Return()--function used to return the single (note:only one) value to the calling method
MessageBox.Show()--use To Create a new window that display which message we gave as input when we press ok button in that window window will closed it is precreated template for message box
Retern-- may be an use created variable or object name or class name or structure,interface,enum name or any other user defined or it may be user defined alias for any function or variable
mani-hellboy
Junior Poster in Training
69 posts since Feb 2012
Reputation Points: 0
Solved Threads: 7
Skill Endorsements: 0
Question Answered as of 1 Year Ago by
skatamatic,
Momerath,
anthonyjpv
and 4 others