Part of the
You Can Learn C# series.
By Ken Brown
Updated: September 5, 2004
This is part 1 of a three part
article on Using Methods, calling a 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 this article we are going to discuss the programming tool known as a Method
call. Back in the earlier days of programming the Method was known as a Procedure
or if it returned a value known as a Function. With C# they are all
called methods, whether a value is returned or not.
You will learn how to pass values to a Method, return values from a Method and
what to do inside a Method. The concept of little sections of code that act as
little black boxes is the foundation of object-oriented programming. The
ability to pass a value into an object and have it return something of value
has lead to programming as we know it today.
Let's start by creating our own little program that utilizes a simple method
and then build to the ability to pass values in and return objects from the
method. To begin create a solution and a project. For information on how to
create a solution and project see our article titled Create
a Project or use a solution you have already created.
So at this point you have created a method called ChangeLabel and it has
produced a change to your form when you ran the application. Now, when you code
you don't have to bunch up all your code into one single event or method. Break
up your code into relevant pieces and make them methods. Then when you call the
methods something will occur and you can call these methods over and over so
you only have to write this code once. This is the foundation of Object Oriented
Programming and will lead you into the mind set of the professional
programmer. Where everything is an object or a little black box with the
ability to get information and / or return information.
I created a new project titled YCLSProj2 within a solution titled
Then to give us some objects to work with drag and drop from the toolbox
a button control, Label control and a TextBox control.
that will be enough to get us started.
Give the Button control an ID property of "btnActivate" and a Text
property of "Activate"
For the Label an ID property of lblAnswer with no Text property.
For the TextBox an ID property of txtInput with no Text
Right click somewhere on WebForm1.aspx and choose menuitem View Code.
Within the class create a new method.
private void ChangeLabel()
lblAnswer.Text = "Change the Label Method.";
When this method is called it will change the text of our label on the form
to read "Change the Label Method."
Now we have to actually call the method. Go to the Page_Load event where you
see the wording "// Put user code to initialize the page here".
Underneath that wording type in ChangeLabel();
You can now Run your application by clicking on the Start button (Blue arrow).
if we did this right you should see an image similar to what is below.
Now let's step through the code and see what is occurring.
While the application is running, Go To the code behind page within Visual
Under the Page_Load event where you placed your ChangeLabel() add a breakpoint
by clicking once on the left side frame. A red dot should show up on the frame
and a red highlight should highlight the Text.
Now hit the Refresh button on your Running application. It should stop where
you placed your breakpoint
Now you want to step into your code by clicking the Step Into button
on the toolbar or if you are emulating C++ mode, choosing the F11 button.
This action will take you right into the method named ChangeLabel
and will stop on the next section of code to be run. (lblAnswer.Text = "Change
the Label Method.";)
To finish up click on the start button.
In part two we continue on this journey, but I will show you how to return
information or objects from a method.
Go To Page 1 /
2 / 3