ASP.NET

The Methods of a Class

When you create a class, the fields are meant to describe it. For a class named House, such aspects as the number of bedrooms, the property type, or its…

6 min read1,283 words5 code samples

Methods Fundamentals#

Introduction#

When you create a class, the fields are meant to describe it. For a class named House, such aspects as the number of bedrooms, the property type, or its value, are used to describe it:

C#
public class House
{
    public char PropertyType;
    public uint Bedrooms;
}

Besides the characteristics used to describe it, an object can also perform actions or assignments. An action performed by a class is called a method. A method is simply a section of code that takes care of a particular detail for the functionality of the class. To create a method, you specify its name. The name of a method is followed by parentheses. Practical Learning: Introducing Methods of a Class

  1. Start Microsoft Visual Web Developer
  2. To create a new web site, on the main menu, click File -> New Web Site...
  3. Make sure the Language is set to Visual C#. Click the second box to the right of Location, delete the suggested name of the web site and replace it with geometry6
  4. Click OK
  5. In the Solution Explorer, right-click Default.aspx and click Rename
  6. Type index.aspx and press Enter
  7. In the Solution Explorer, right-click geometry6 and click Add New Item...
  8. In the Templates list, click Class
  9. Replace the suggested name with Cylinder
  10. Click Add
  11. Read the message box that comes up and click Yes
  12. Change the class as follows: using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for Cylinder /// </summary> public class Cylinder { public double rad; public double hgt; }
  13. Click the index.aspx tab
  14. Change its code as follows: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title> Geometry: Cylinder </title> </head> <body> <% var Cup = new Cylinder(); Cup.rad = 22.08; Cup.hgt = 28.14; Response.Write("=-=Cylinder Characteristics=-="); Response.Write("<br>Radius: "); Response.Write(Cup.rad); Response.Write("<br>Height: "); Response.Write(Cup.hgt); %> </body> </html>
  15. Save the file

Creating a Method#

A method's job is to carry a specific assignment within a program. As such, it could provide a value once the assignment has been carried. In some cases, a method must produce a result. If it doesn't, then it is considered void. The type of value that a method can provide (or return) is written on the left side of the method name. If the method doesn't produce a result, type void to its left. The assignment that a method carries is included between an opening curly bracket "{" and a closing curly bracket "}". Here is an example:

C#
public class House
{
    public char PropertyType;
    public uint Bedrooms;

    void Display()
    {
    }
}

Like a member variable:

  • If the method will be accessed only by members of the same class, you can mark it as private or not mark it with an access keyword
  • If the method will be accessed by members of the class and other parts of the same program but not outside of the program, mark it with the internal keyword
  • In the method can be accessed by the members of the same class, other parts of the same program, and other parts of other programs, mark it as public

After creating a method, in its body delimited by its curly brackets, you can define the desired behavior. In the same way, you can create as many methods as you want in a class. To assist you with managing the methods of a class, the Code Editor is equipped with two combo boxes. The left combo box, called Types, displays the classes that are part of the project. The right combo box, named Members, displays the members of the class that is selected in the Types combo box. If you select an item from the Members combo box, the caret would be displayed on its declaration in the file.

The Solution Explorer#

The Solution Explorer is a window that displays the file names and other items used in your project. The items of this window display in a tree. To expand a node, you can click its + button. To collapse it, click its - button. To explore an item, you can double-click it. The result depends on the item you double-clicked. The Solution Explorer can be used to create and add a new class or a new item to the current project. It can also be used to add a another project to the current project. To perform any of these operations, you can right-click a folder node such as the name of the project and position the mouse on Add to select the desired operation. Remember that you can also perform any of these operations from the Project category of the main menu. The Solution Explorer also allows you to rename or delete some of the items that belong to your project.

Accessing a Method#

After creating a method, you can access it outside of its class. You do this following the same rules used to access a field, with the period operator. Unlike a field, the name of a class must be followed by parentheses.

Methods and Local Variables#

Introduction#

In the body of a method, you can declare one or more variables that would be used only by the method. A variable declared in the body of a method is referred to as a local variable. The variable cannot be accessed outside of the method it belongs to. After declaring a local variable, it is made available to the method and you can use it as you see fit, for example, you can assign it a value prior to using it.

A Method that Returns a Value#

If a method has carried an assignment and must make its result available to other methods or other classes, the method must return a value and cannot be void. To declare a method that returns a value, provide its return type to the left of its name. Here is an example:

C#
class Exercise
{
    double Operation()
    {
    }
}

After a method has performed its assignment, it must clearly demonstrate that it is returning a value. To show this, you use the return keyword followed by the value that the method is returning. The value returned must be of the same type specified as the return type of the method. Here is an example:

C#
class Exercise
{
    double Operation()
    {
	return 24.55;
    }
}

A method can also return an expression, provided the expression produces a value that is conform to the return type. Here is an example:

C#
class Exercise
{
    double Operation()
    {
	return 24.55 * 4.16;
    }
}

When a method returns a value, the compiler considers such a method as if it were a regular value. This means that you can use Response.Write() method to display its value. To do this, simply type the name of the method and its parentheses in the Response.Write() parentheses. Of course you can use the var keyword to declare an instance of the class. We have seen how to call a method from the parentheses of another method that needs its result. In the same way, a method that returns a value can be assigned to a variable of the same type. Practical Learning: Creating Methods

  1. Click the Cylnder.cs tab
  2. To create a method, change the Cylinder class as follows: using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for Cylinder /// </summary> public class Cylinder { public double rad; public double hgt; public double Volume() { return this.rad * this.rad * this.hgt * 3.14159; } }
  3. Save the file
  4. Click the index.aspx tab
  5. To access the method, change the code as follows: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Geometry: Cylinder</title> </head> <body> <% var Cup = new Cylinder(); Cup.rad = 22.08; Cup.hgt = 28.14; Response.Write("=-=Cylinder Characteristics=-="); Response.Write("<br>Radius: "); Response.Write(Cup.rad); Response.Write("<br>Height: "); Response.Write(Cup.hgt); Response.Write("<br>Volume: "); Response.Write(Cup.Volume()); %> </body> </html>
  6. Save the file
  7. Press Ctrl + F5 to see the result
  8. Close the browser

Previous Copyright © 2009 C# Key Next