Home Page

Click here to download the sample source code for this Methods lab exercise  If you forgot how to open the files using Visual Web Developer, click here to learn How to open C# ASP.NET Website files.  Please use these files only as a reference.  You should go through the exercises yourself and create your own project according to the instructions in the exercise. 

Now that we have seen how the C# list data types work, it’s time to move on to .  Methods are blocks of code that perform some actions and can return back a result.  Methods are also called Functions or Procedures in other languages.  Methods are a good way to organize your code into separate functional areas in order to make it more maintainable.  Methods can also help you reduce redundancy by taking identical blocks of code that you have in multiple places of your program and keep them in one central place.

Create a new website using Visual Web Developer and call the project Lab4.  Go to the Design view for the file Default.aspx.  Drag a new Button onto the form and change the Text property to say “Execute a Method”.  Drag a new TextBox to the right of the Button control.  Double click the Button to create the event handler method.  Replace the code in the method Button1_Click with the following:

 

protected void Button1_Click(object sender, EventArgs e)
{
 int iResult = AddTwoNumbers();
 TextBox1.Text = iResult.ToString();
}
int AddTwoNumbers()
{
 int iNum1 = 50;
 int iNum2 = 20;

 return iNum1 + iNum2;
}

Let’s go over how to declare a Method.  A Method declaration consists of three parts:
int AddTwoNumbers()
First is the return type.  In the example above, the return type is “int” or Integer, so I declared that my Method will return a number.
Second is the name of the Method, which is “AddTwoNumbers” in my example.
The last part of a Method declaration is a list of parameters or arguments to the Method.  The parameters are specified inside parentheses and are comma separated.  We will see an example of how to specify parameters later in this exercise.  In my example I do NOT have any Method parameters so you only see open and closed parentheses following the Method name ().

Now let’s discuss the contents of my Method AddTwoNumbers.  AddTwoNumbers is a very simple Method consisting of three lines of code.  Inside the Method I declare and initialize two variables iNum1 and iNum2.  I then add the two numbers together using the addition operator + and return the result of the addition to the Method caller.  Since I declared that my Method will return a number, it is mandatory that I have a “return” statement in my Method.  If I don’t put a “return” statement, the source code compiler will throw an error. 

In order to use my Method I simply put the name of the Method AddTwoNumbers followed by parentheses in the code block where it is needed.  Using a Method is also sometimes referred to as “calling the Method”, so the block of code that ”calls” the Method is thus referred to as the “Method caller”.  In my code example above, the Method caller can be found on the following line inside Button1_Click:
int iResult = AddTwoNumbers();
Notice that I called the Method by using the expression “AddTwoNumbers()”.  Since the Method returns a number I assigned the expression to a variable named “iResult” which is an Integer.  Finally I showed the result of the addition in a Textbox control.

Now let’s do an exercise where we declare a Method that doesn’t have any return type.  This type of Method is used when you want the Method to perform some actions, but don’t need any data back from the Method.  Create a new form called Default2.aspx.  In case you forgot how to do this, click here to watch an example video.  Go to the Design view and drag a Button onto the Form.  Change the Text of the button to say “Execute a Method with NO return”.  Drag a Textbox onto the Form.  Double click the Button to create the event handler and put the following:

 

protected void Button1_Click(object sender, EventArgs e)
{
 int iNum1 = 50;
 int iNum2 = 20;

 int iResultNumber = iNum1 + iNum2;
 DisplayResult(iResultNumber);
}
void DisplayResult(int iResult)
{
 TextBox1.Text = iResult.ToString();
}

In the above example I created a Method named DisplayResult.  This time around however my Method does NOT return any data so I had to put “void” for the return type.  Another difference in this example is that my Method takes one argument “iResult” which is an Integer.  This means that whenever my Method is called, I have to pass a number to the Method.  You can see that I did that in my example on the line:
DisplayResult(iResultNumber);
Since my Method does not return any data, I did not use the “return” keyword in the Method.

Let’s do an example with a Method that takes multiple arguments.  Create a new form called Default3.aspx.  Go to the Design view and drag a Button onto the Form.  Change the Text of the button to say “Execute a Method with multiple arguments”.  Drag a Textbox onto the Form.  Double click the Button to create the event handler and put the following:

 

protected void Button1_Click(object sender, EventArgs e)
{
 AddTwoNumbersAndDisplayResult(50, 20);
}
void AddTwoNumbersAndDisplayResult(int iNumber1, int iNumber2)
{
 int iResultNumber = iNumber1 + iNumber2;
 TextBox1.Text = iResultNumber.ToString();
}

Notice how this time I created a new Method named “AddTwoNumbersAndDisplayResult” and it takes 2 arguments iNumber1 and iNumber2.  To declare multiple arguments, you simply separate the argument variable names with a comma.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment