Home

The Methods of a Class

 

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.

Practical Learning Practical Learning: Using a Method's Local Variables

  1. Start Microsoft Visual C# and create a Console Application named Geometry1
  2. To create a new class, on the main menu, click Project -> Add Class...
  3. Set the Name to Cylinder and click Add
  4. To declare and use local variables of a method, change the file as follows:
     
    using System;
    
    namespace Geometry1
    {
        class Cylinder
        {
            public void Process()
            {
                double Radius, Height;
                double BaseArea, LateralArea, TotalArea;
                double Volume;
    
                Console.WriteLine("Enter the dimensions of the cylinder");
                Console.Write("Radius: ");
                Radius = double.Parse(Console.ReadLine());
                Console.Write("Height: ");
                Height = double.Parse(Console.ReadLine());
    
                BaseArea = Radius * Radius * Math.PI;
                LateralArea = 2 * Math.PI * Radius * Height;
                TotalArea = 2 * Math.PI * Radius * (Height + Radius);
                Volume = Math.PI * Radius * Radius * Height;
    
                Console.WriteLine("\nCylinder Characteristics");
                Console.WriteLine("Radius:  {0}", Radius);
                Console.WriteLine("Height:  {0}", Height);
                Console.WriteLine("Base:    {0:F}", BaseArea);
                Console.WriteLine("Lateral: {0:F}", LateralArea);
                Console.WriteLine("Total:   {0:F}", TotalArea);
                Console.WriteLine("Volume:  {0:F}", Volume);
            }
        }
    }
  5. Access the Program.cs file and change it as follows:
     
    using System;
    
    namespace Geometry1
    {
        public class Program
        {
            static void Main()
            {
                Cylinder cyl = new Cylinder();
    
                cyl.Process();
                Console.WriteLine();
            }
        }
    }
  6. Execute the application to test it. Here is an example:
     
    Enter the dimensions of the cylinder
    Radius: 38.64
    Height: 22.48
    
    Cylinder Characteristics
    Radius:  38.64
    Height:  22.48
    Base:    4690.55
    Lateral: 5457.75
    Total:   14838.85
    Volume:  105443.65
  7. Close the DOS window

 

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:

using System;

class Exercise
{
    static double Operation()
    {
    }

    static void Main()
    {
    }
}

After a method has performed its assignment, it must clearly demonstrate that it is returning a value. To do 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:

using System;

class Exercise
{
    static double Operation()
    {
	return 24.55;
    }

    static void Main()
    {
    }
}

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

using System;

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

    static void Main()
    {
    }
}

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 the Console.Write() or the Console.WriteLine() method to display its value. To do this, simply type the name of the method and its parentheses in the Console.Write() of the Console.WriteLine() methods' parentheses. Here is an example:

using System;

class Exercise
{
    static double Operation()
    {
	return 24.55;
    }

    static void Main()
    {
	Console.WriteLine(Operation());
    }
}

In the same way, a method that returns a value can be assigned to a variable of the same type.

Practical Learning Practical Learning: Returning a Value From a Method 

  1. Access the Cylinder.cs file
  2. To create methods that return values, change the file as follows:
     
    using System;
    
    namespace Geometry1
    {
        class Cylinder
        {
            public double GetRadius()
            {
                double rad;
    
                Console.Write("Radius: ");
                rad = double.Parse(Console.ReadLine());
    
                return rad;
            }
    
            public double GetHeight()
            {
                double h;
    
                Console.Write("Height: ");
                h = double.Parse(Console.ReadLine());
    
                return h;
            }
    
            public void Process()
            {
                double Radius, Height;
                double BaseArea, LateralArea, TotalArea;
                double Volume;
    
                Console.WriteLine("Enter the dimensions of the cylinder");
                Radius = GetRadius();
                Height = GetHeight();
    
                BaseArea = Radius * Radius * Math.PI;
                LateralArea = 2 * Math.PI * Radius * Height;
                TotalArea = 2 * Math.PI * Radius * (Height + Radius);
                Volume = Math.PI * Radius * Radius * Height;
    
                Console.WriteLine("\nCylinder Characteristics");
                Console.WriteLine("Radius:  {0}", Radius);
                Console.WriteLine("Height:  {0}", Height);
                Console.WriteLine("Base:    {0:F}", BaseArea);
                Console.WriteLine("Lateral: {0:F}", LateralArea);
                Console.WriteLine("Total:   {0:F}", TotalArea);
                Console.WriteLine("Volume:  {0:F}", Volume);
            }
        }
    }
  3. Execute the application. Here is an example:
     
    Enter the dimensions of the cylinder
    Radius: 52.08
    Height: 36.44
    
    Cylinder Characteristics
    Radius:  52.08
    Height:  36.44
    Base:    8521.02
    Lateral: 11924.20
    Total:   28966.25
    Volume:  310506.14
  4. Close the DOS window
 

Home Copyright © 2006-2007 FunctionX, Inc. Next