We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,613 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

What is the difference between...

What is the difference between "

return

" and "

messageBox.Show("whatever")

"?
Also explain(efforts will be appreciated):
What does

retern

do?

9
Contributors
17
Replies
2 Weeks
Discussion Span
1 Year Ago
Last Updated
18
Views
Question
Answered
jackbauer24
Posting Whiz in Training
244 posts since Oct 2011
Reputation Points: 21
Solved Threads: 3
Skill Endorsements: 0

Also explain(efforts will be appreciated):
What does
C# Syntax (Toggle Plain Text)
retern
do?

I meant

return

.

jackbauer24
Posting Whiz in Training
244 posts since Oct 2011
Reputation Points: 21
Solved Threads: 3
Skill Endorsements: 0

And how to use

return
jackbauer24
Posting Whiz in Training
244 posts since Oct 2011
Reputation Points: 21
Solved Threads: 3
Skill Endorsements: 0
MessageBox.Show("MessageGoesHere");

This, when running a windows forms application, will display a popup message box on screen displaying the information you have passed it within the brackets.

return x;

Return is used to send a variable back from a method to the block of code that called the method. It is a required line of code for methods that are not declared as void (returns nothing)

For example:

int a = 10;
int b = 5;

int d = Addition(a, b);

public int Addition(int Num1, int Num2) //Method returning a parameter of type Integer
{
    int c = Num1 + Num2;
    return c;
}

In the above example we have two integers, a and b, which I am passing to a simple addition method that will add them together and give me back a value to set integer d too.

In this method the return keyword plays the role of passing the value of 'c' back to the original location the method was called from, in this case line 4.

MikeyIsMe
Master Poster
798 posts since Nov 2010
Reputation Points: 88
Solved Threads: 69
Skill Endorsements: 10

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

OK. Thank you for all the replies you guys gave. So return basically "gives back a value" to the line that called the method. So in this kind of syntax in a class called "main.cs"--

public static int Subtraction(int num1, int num2)
{
    int result = num1 - num2;
    return result;
}

And then later I call the method like this on a button click---

int sub = main.Subtraction(5, 3)
MessageBox.Show("5 - 3 is equal to " +sub)

return value is given to the line

int sub = main.Subtraction(5, 3)

and then used in the line

MessageBox.Show("5 - 3 is equal to " +sub)

.
So if the

MessageBox.Show()

line wasn't there, the return value would not have been displayed?

Also explain:
I just learnt that staticmethods can be used without making any objects. So-
1. Why wouldn't I want ALL my classes and methods to be static?
2. Why is Main() static?

jackbauer24
Posting Whiz in Training
244 posts since Oct 2011
Reputation Points: 21
Solved Threads: 3
Skill Endorsements: 0

1. Static classes can only exist as singleton classes. Say you wanted a class for monsters in a game. If that class was entirely static, each monster would share the same members and methods. Therefore all monsers would occupy the same coordinates, life, etc. Static classes and methods are usually used as helper classes or functions that are common to all of the classes instances. For example, a class that you use to load data from a settings file. You only need 1 instance of this class, and it just helps keep all of your settings file operations in a neat little class.

2. Main() is static since it is common to all instances of your program. Your application itself is a static class, and so is its application domain. But your main() function is always the same between all of them. It basically boils down to your compiler needing an entry point into your program. You can put the [STAThread] static main() anywhere in your program, try putting it in your main form in a windows app. Since it's static it doesn't need an instance of the class to exist, and can be used as the entry point from within any class.

skatamatic
Posting Shark
986 posts since Nov 2007
Reputation Points: 403
Solved Threads: 132
Skill Endorsements: 1

IMPORTANT: I USE VISUAL STUDIO 2008 EXPRESS EDITION C# ide

Since it's static it doesn't need an instance of the class

So if I change the "Main()" in my "Program.cs" to "NotMain()" or whatever and i place Main in another class called "ThisIsMain.cs", ThisIsMain becomes my entry point?

jackbauer24
Posting Whiz in Training
244 posts since Oct 2011
Reputation Points: 21
Solved Threads: 3
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

Ok. I get all this return business. But what does return 0 do?

jackbauer24
Posting Whiz in Training
244 posts since Oct 2011
Reputation Points: 21
Solved Threads: 3
Skill Endorsements: 0

Ok. I get all this return business. But what does return 0 do?

Returns a value of 0 to the calling method...

skatamatic
Posting Shark
986 posts since Nov 2007
Reputation Points: 403
Solved Threads: 132
Skill Endorsements: 1

Why will I even need return 0;?

jackbauer24
Posting Whiz in Training
244 posts since Oct 2011
Reputation Points: 21
Solved Threads: 3
Skill Endorsements: 0

Why will I even need return 0;?

You don't. In C++ most compilers require an int main(), whereas in C# I am pretty sure all compilers support a void main().

A return value of 0 from your main() typically indicates that the program executed properly and returned with no error.

skatamatic
Posting Shark
986 posts since Nov 2007
Reputation Points: 403
Solved Threads: 132
Skill Endorsements: 1

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

return is used to return a value from the method, Suppose if x is an integer valve in any method named xImplement() then return x; will return the value x to its caller. And if no value is returned i.e. return; is used without any values then return is used to prevent the further execution of code.

EBS.VivekGupta
Newbie Poster
9 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 1 Year Ago by skatamatic, Momerath, anthonyjpv and 4 others

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1210 seconds using 2.87MB