Part of the
You Can Learn C# series.
By Ken Brown
Editor, YouCanLearnSeries.com
Updated: September 5, 2004
This is part 2 in a three part
article on Using Methods, calling a Method, passing values to the Method and
returning values from a method.
Using Methods and Calling a Method in C#
Passing Objects to a Method
Returning Values from a Method.
In part two of this article we are looking at returning values from a Method
call. From page one we mentioned this is the old concept known as a Function.
I will show you simple returned values such as strings and boolean
values and then how to maximize those return values in your method calls. You
will also learn how to pass values to the method.
-
Let's begin by learning how to pass a value to the Method.
-
Go back to the code behind in Visual Studio.
-
In the Page_Load Event type in ChangeLabel("Hello Chicago"); where you
originally had only the words ChangeLabel(); What we are trying to do is to
pass in the words "Hello Chicago" to the ChangeLabel
Method.
-
Now we have to change the Method to receive values.
Go to the code where we defined the ChangeLabel method.
Looks like this: private void ChangeLabel()
We are going to change it to accept a string object.
After the Parenthesis in ChangeLabel add (string strLabel)
-
The Method definition now looks like private void ChangeLabel(string strLabel).
The Method is expecting you to pass in a string object, before it will do
anything for you.
-
Change the next line in the code to use the value passed into the method
lblAnswer.Text = strLabel;
-
Run the application and you should see "Hello Chicago" in the label.
Sending values from other controls to the Method.
The next step is to advance the Method call one minor step where we send a
value that we type into the textbox on the form. Now just typing in the
words you want to show up on the label isn't going to be enough. You will have
to activate an event. We will place the event on the Activate button control.
-
Go back to design view of the form.
Double Click the Activate Button control which takes you to the EventHandler
for the button control.
-
We will call the ChangeLabel method, but we will pass in the value in the TextBox
control. To capture the value from the textbox control use txtInput.Text.
-
We then change our Method call to read: ChangeLabel(txtInput.Text.ToString());
-
Run the application.
-
Type in "Hello and your city name" into the textbox.

-
Then click on the Activate button.
Whatever you type in the textbox will show up in the label.

If you are having problems understanding the flow, set a breakpoint in the
Activate Event and step through the code.
What Kind of Objects can be Passed to a Method?
What can you pass to a Method? Good question and the answer is just about any
object supported by C#. You can send strings, DataSets, DataGrids, structs,
collections, arrays and Numbers. You must remember you are passing by value and
the scope of the is the same as the scope of the Method. Meaning when the
method is finished, any calculations or changes to passed values end.
For example, you send in an int with a value of 15. You multiply that
value by three and you now have an int value of 45. Then the method ends, and
your value of 45 ends with it, unless you want to return the calculated number
back to the object that called the Method. For more information on returning
objects back from a method go on to Part 3, Returning
Values from a Method.
Go To Page 1 /
2 / 3
|