C#. Structures and methods. Initialization of structures. Constructors in structures. Properties in structures




Structures and methods. Initialization of structures. Constructors in structures. Properties in structures


Contents


Search other websites:

1. An example of a structure in which methods are declared

A structure is given that describes the subscriber data in the telephone directory. The structure is called Telephone. In the structure are implemented 5 fields and 11 methods that operate these fields. Also, there is a constructor in the structure that initializes the value of the structure fields. The fields of the structure are declared as hidden, that is, they have a private access identifier. The methods and the constructor have the public access identifier and are publicly available.
Below is the code for the such structure

struct Telephone
{
    // the fields of structure are hidden
    private string number; // phone number
    private string name; // subscriber name
    private string surname; // subscriber surname
    private string address; // address
    private int code; // postal code

    // A constructor that initializes the fields of structure
    public Telephone(string Number, string Name, string Surname, string Address, int Code)
    {
        // fill of variables - must fill in all fields of the structure
        number = Number;
        name = Name;
        surname = Surname;
        address = Address;
        code = Code;
    }

    // methods for accessing structure fields
    // reading data
    public string Get_Number() { return number; }
    public string Get_Name() { return name; }
    public string Get_Surname() { return surname; }
    public string Get_Address() { return address; }
    public int Get_Code() { return code; }

    // filling of structure fields (data)
    public void Set_Number(string Number) { number = Number; }
    public void Set_Name(string Name) { name = Name; }
    public void Set_Surname(string Surname) { surname = Surname; }
    public void Set_Address(string Address) { address = Address; }
    public void Set_Code(int Code) { code = Code; }
}

An example of the use of structure methods for operating the data of structure:

// demonstration of the use of methods
Telephone T1;

// initializing fields using the constructor
T1 = new Telephone("900293", "Johnson ", "John", "Moscow", 3902);

string Number, Name, Surname, Address;
int Code;

Number = T1.Get_Number(); // Number = "900293"
Name = T1.Get_Name(); // Name = "Johnson"
Code = T1.Get_Code(); // Code = 3902

// Set the new Number value
T1.Set_Number("777777");
Number = T1.Get_Number(); // Number = "777777"

// Set the new Code value
T1.Set_Code(12345);
Code = T1.Get_Code(); // Code = 12345

 

2. How can you initialize the fields (data) of a structure? Example

For convenient initialization of data fields in the structure, you can declare an explicitly defined constructor.
Example. Implementation an explicitly defined constructor for a Point type structure that describes a point on the coordinate plane. The structure declaration is as follows.

struct Point
{
    public int x, y;

    // explicitly defined constructor
    public Point(int nx, int ny)
    {
        x = nx;
        y = ny;
    }
}

A call to the constructor when the instance of the structure is declared.

Point P = new Point(5, 6); // P.x = 5; P.y = 6

 

3. Is it possible to declare a structure in a default constructor?

A default constructor or a constructor without parameters can not be declared in a structure. For all structures, the system determines the default constructor automatically. Therefore, there is no need for an additional declaration of the default constructor.



 

4. Is it possible to declare a destructor in the structure?

You can not declare a destructor in a structure.

 

5. How do I declare an explicitly defined constructor in a structure? Example

In this example, a Date structure is implemented, in which there is a constructor that initializes the fields of the structure with values.

struct Date
{
    public int day; // date
    public int month; // month
    public int year; // year

    public Date(int nday, int nmonth, int nyear)
    {
        day = nday;
        month = nmonth;
        year = nyear;
    }
}

An example of using this constructor in an event handler

private void button3_Click(object sender, EventArgs e)
{
    Date d = new Date(2, 3, 2001); // call the constructor

    // outputting the fields of structure to the form
    label1.Text = d.day.ToString();
    label2.Text = d.month.ToString();
    label3.Text = d.year.ToString();
}

 

6. Example of a structure containing properties

To use properties in a structure, you need to create an instance of the structure using the new operator.

Let a structure describing a point on the coordinate plane be declared in a certain class. The structure includes:

  • two internal variables x, y;
  • method SetXY(), which sets new values in the variables x, y;
  • two properties X, Y. These properties provide reading (get) and writing (set) internal variables x, y.
// A structure that describes a point on the coordinate plane
struct Point
{
    int x, y;

    public void SetXY(int nx, int ny)
    {
        x = nx;
        y = ny;
    }

    // property X
    public int X
    {
        get { return x; }
        set { x = value; }
    }

    // property Y
    public int Y
    {
        get { return y; }
        set { y = value; }
    }
}

Below is the use of the Point structure from the program code of some class method.

// the structure is initialized by the operator new
Point P = new Point();

// write the values 25, 30 using the properties
P.X = 25;
P.Y = 30;

int t;
t = P.Y; // t = 30

It is important here that an instance of the structure be created using the new operator.

 


Related topics