C#. Using the delegates that reference the instance methods of the class. Example




Part 2. Using the delegates that reference the instance methods of the class. Example

This theme is based on the theme:


Contents


Search other websites:

1. What steps need to be performed so that delegates refer to the instance methods of an object of some class?

If methods are declared in the class as non-static (without the static keyword), then you need to create an instance of the class object. This means that you need to allocate memory for the object (instance) of the class (and its methods) using the new operator.

If methods, to which a delegate can refer to, are described in a class named X, then the sequence of steps is as follows:

  • 1. Declare the type of the delegate outside the class X.
  • 2. In the required program code (for example, the event handler):
    • create an instance of an object of class X in the standard way (using the new operator);
    • initialize the delegate in the standard way (assign a method to the delegate from the created class instance);
    • call the class method using the delegate.

 

2. An example of using a delegate that references the methods of an instance of a class

This example is for applications that are created using the Windows Forms Application template. An example of creating an application of the Windows Forms Application type is described in detail in the article:



2.1. The task

For applications such as Windows Forms Application, declare the type of the delegate that refers to the method. The requirements for the method signature are as follows:

  • the method gets a variable of type double;
  • the method returns a ‘double’ value, which is the result of the calculation in accordance with the condition of the task.

Implement the call of the three methods using the delegate, which get the radius R as the input parameter and compute:

  • the circumference by a formula D = 2 · π · R;
  • the area of a circle by a formula S = π · R2;
  • the volume of a sphere by a formula V = 4/3 · π · R3.

Methods must be presented in a separate class as non-static (without the static keyword). It is desirable that the class be declared in a separate module (file).

 

2.2. Considerations

For the work of the program we choose the following names:

  • name of delegate type – CalcFigures;
  • name of the delegate (object) is CF;
  • name of class – Figures;
  • name of the file in which the class is declared – “Figures.cs”;
  • name of the method that calculates the length of the circle – Get_Length();
  • name of the method that computes the area of a circle – Get_Area();
  • name of the method that calculates the volume of a sphere – Get_Volume().

 

2.3. Solution (code snippets)

2.3.1. Declaring the delegate type

The delegate type is declared in the class that hosts the delegate demonstration method (for example, the event handler in applications such as Windows Forms Application). In the case of the Windows Forms Application, the main class is the main form class Form1. In the body of this class, you need to declare a delegate. The approximate text code of the module Form1.cs has the following form:

namespace TrainDelegates02
{
    public partial class Form1 : Form
    {
        // Declaring the delegate type. Type is called CalcFigures
        delegate double CalcFigures(double r);

        public Form1()
        {
            InitializeComponent();
        }

        ...

    }
}

 

2.3.2. Declaring of the class, which is placed in a separate module “Figures.cs”

In Microsoft Visual Studio, you can declare a class in a separate module. To ensure the convenience of application development, it is recommended to place the class in a separate module (for example, “Figures.cs”).

The source code of the class is as follows:

// class, is placed in a separate module Figures.cs
class Figures
{
    // class methods declaration
    // methods are declared without the static keyword
    // the circumference
    public double Get_Length(double r)
    {
        double length;
        length = 3.1415 * 2 * r;
        return length;
    }

    // area of a circle
    public double Get_Area(double r)
    {
        double area;
        area = 3.1415 * r * r;
        return area;
    }

    // volume of a sphere
    public double Get_Volume(double r)
    {
        double volume;
        volume = 4.0 / 3.0 * 3.1415 * r * r * r;
        return volume;
    }
}

 

2.3.3. Calling methods using delegate

Demonstration of calling methods using a delegate from another program code, for example, from the button click event handler (the Windows Forms Application template).

// click on the "Calculate" button
private void button1_Click(object sender, EventArgs e)
{
    // A demonstration of working with methods that are instances of a class
    // declaration of variables
    double radius;
    double length, area, volume;

    // declaration of an instance of the class (if the methods are static, then you do not need to do this)
    Figures FG;         // FG - variable (object) of type "class Figures"
    FG = new Figures(); // create an instance of a class object

    // declaration of a delegate named CF of type CalcFigures
    CalcFigures CF;

    // read the radius
    radius = Convert.ToDouble(textBox1.Text);

    // calculate the values of the circumference
    // initialize the delegate by the value of the method that computes the length of the circle
    CF = FG.Get_Length;
    length = CF(radius);

    // display the result of the circumference
    label2.Text = length.ToString();

    // calculate the value of the area of a circle
    CF = FG.Get_Area; // assign the method Get_Area to the delegate
    area = CF(radius);
    label3.Text = area.ToString();

    // calculate the volume of a sphere
    CF = FG.Get_Volume; // assign the method Get_Volume to the delegate
    volume = CF(radius); // call the Get_Volume method
    label4.Text = volume.ToString();
}

As you can see from the program code, you must create an instance of the class object to use the methods of this class in the program.

 


Related topics