Home

Techniques of Using Structures

 

A Structure as a Property

Once a structure exists, you can use it like a data type. For example, you can create a property that is a structure type. The rules are the same we reviewed for creating a property of a class. After creating the property, you can use it as you see fit. Here is an example:

using System;

public struct Real
{
    private double val;

    public double Value
    {
        get { return val; }
        set { val = value; }
    }

    public double Read()
    {
        return double.Parse(Console.ReadLine());
    }
}
public struct Rectangle
{
    Real len;
    Real hgt;

    public Real Length
    {
        get { return len; }
        set { len = value; }
    }

    public Real Height
    {
        get { return hgt; }
        set { hgt = value; }
    }

    public void CreateRectangle()
    {
        Real rat = new Real();

        Console.WriteLine("Enter the dimensions of the rectangle");
        Console.Write("Enter the length: ");
        len.Value = rat.Read();
        Console.Write("Enter the height: ");
        hgt.Value = rat.Read();
    }
}

public class Program
{
    static int Main()
    {
        Rectangle rect = new Rectangle();

        rect.CreateRectangle();
        Console.WriteLine();

        Console.WriteLine("Rectangle Characteristics");
        Console.WriteLine("Length:    {0}", rect.Length.Value);
        Console.WriteLine("Height:    {0}", rect.Height.Value);
        Console.WriteLine("Perimeter: {0}",
            (rect.Length.Value + rect.Height.Value) * 2);
        Console.WriteLine("Area:      {0}",
            rect.Length.Value * rect.Height.Value);

        return 0;
    }
}

Here is an example of running the program:

Enter the dimensions of the rectangle
Enter the length: 44.84
Enter the height: 26.75

Rectangle Characteristics
Length:    44.84
Height:    26.75
Perimeter: 143.18
Area:      1199.47
Press any key to continue . . .

Returning a Structure From a Method

Like regular data type or a class, a structure can serve as the return type of a method.  The rules are more related to those of a class. When creating the method, type the name of the structure on the left side of the name of the method. In the body of the method, implement the desired behavior. Before exiting the method, make sure you return a valid value that is of the type of the structure. When a method returns a value of the type of a structure, you can assign the method to a variable of the type of the structure.

Here is an example of implementing a method that returns a structure type, including calling the method and using its value:

using System;

public struct Real
{
    private double val;

    public double Value
    {
        get { return val; }
        set { val = value; }
    }

    public double Read()
    {
        return double.Parse(Console.ReadLine());
    }
}

public struct Rectangle
{
    Real len;
    Real hgt;

    public Real Length
    {
        get { return len; }
        set { len = value; }
    }

    public Real Height
    {
        get { return hgt; }
        set { hgt = value; }
    }

    public void CreateRectangle()
    {
        Real rat = new Real();

        Console.WriteLine("Enter the dimensions of the rectangle");
        len = GetLength();
        Console.Write("Enter the height: ");
        hgt.Value = rat.Read();
    }

    public Real GetLength()
    {
        Real rat = new Real();

        Console.Write("Enter the length: ");
        rat.Value = rat.Read();
        return rat;
    }
}

public class Program
{
    static int Main()
    {
        Rectangle rect = new Rectangle();

        rect.CreateRectangle();
        Console.WriteLine();

        Console.WriteLine("Rectangle Characteristics");
        Console.WriteLine("Length:    {0}", rect.Length.Value);
        Console.WriteLine("Height:    {0}", rect.Height.Value);
        Console.WriteLine("Perimeter: {0}",
            (rect.Length.Value + rect.Height.Value) * 2);
        Console.WriteLine("Area:      {0}",
            rect.Length.Value * rect.Height.Value);

        return 0;
    }
}

Here is an example of running the application:

Enter the dimensions of the rectangle
Enter the length: 36.04
Enter the height: 22.86

Rectangle Characteristics
Length:    36.04
Height:    22.86
Perimeter: 117.8
Area:      823.8744
Press any key to continue . . .

Passing a Structure as Argument

Like a data type, a structure can be passed as argument to a method. The argument is primarily passed as done for a class. After passing the argument, in the body of the method, you can access the public members of the structure, using the period operator. Here is an example:

using System;

public struct Real
{
    private double val;

    public double Value
    {
        get { return val; }
        set { val = value; }
    }

    public double Read()
    {
        return double.Parse(Console.ReadLine());
    }
}

public struct Rectangle
{
    Real len;
    Real hgt;

    public Real Length
    {
        get { return len; }
        set { len = value; }
    }

    public Real Height
    {
        get { return hgt; }
        set { hgt = value; }
    }

    public void CreateRectangle()
    {
        Real rat = new Real();

        Console.WriteLine("Enter the dimensions of the rectangle");
        len = GetLength();
        Console.Write("Enter the height: ");
        hgt.Value = rat.Read();
    }

    public Real GetLength()
    {
        Real rat = new Real();

        Console.Write("Enter the length: ");
        rat.Value = rat.Read();
        return rat;
    }
}

public class Program
{
    public static void ShowCharacteristics(Rectangle rect)
    {
        Console.WriteLine("Rectangle Characteristics");
        Console.WriteLine("Length:    {0}", rect.Length.Value);
        Console.WriteLine("Height:    {0}", rect.Height.Value);
        Console.WriteLine("Perimeter: {0}",
		(rect.Length.Value + rect.Height.Value) * 2);
        Console.WriteLine("Area:      {0}",
		rect.Length.Value * rect.Height.Value);
    }
    static int Main()
    {
        Rectangle r = new Rectangle();
        r.CreateRectangle();
        
        Console.WriteLine();
        ShowCharacteristics(r);

        return 0;
    }
}

Here is an example of running the program:

Enter the dimensions of the rectangle
Enter the length: 114.55
Enter the height: 82.72

Rectangle Characteristics
Length:    114.55
Height:    82.72
Perimeter: 394.54
Area:      9475.576
Press any key to continue . . .

When you pass a structure to a method as we did above, it referred to as passing by value. A copy of the value of the structure is passed to the method. If the method modifies the argument, the original variable would stay intact. If you want the method to modify the value of the structure, you can pass the argument by reference. You can do this using the (rules of the) ref and the out keywords.

Here is an example of passing a structure by reference using the ref keyword:

using System;

public struct Real
{
    private double val;

    public double Value
    {
        get { return val; }
        set { val = value; }
    }

    public double Read()
    {
        return double.Parse(Console.ReadLine());
    }
}

public struct Rectangle
{
    Real len;
    Real hgt;

    public Real Length
    {
        get { return len; }
        set { len = value; }
    }

    public Real Height
    {
        get { return hgt; }
        set { hgt = value; }
    }

    public void CreateRectangle()
    {
        Console.WriteLine("Enter the dimensions of the rectangle");
        len = GetLength();
        GetHeight(ref hgt);
    }

    public Real GetLength()
    {
        Real rat = new Real();

        Console.Write("Enter the length: ");
        rat.Value = rat.Read();
        return rat;
    }

    public void GetHeight(ref Real rl)
    {
        Real rat = new Real();

        Console.Write("Enter the height: ");
        rl.Value = rat.Read();
    }
}

public class Program
{
    static int Main()
    {
        Rectangle rect = new Rectangle();

        rect.CreateRectangle();
        Console.WriteLine();

        Console.WriteLine("Rectangle Characteristics");
        Console.WriteLine("Length:    {0}", rect.Length.Value);
        Console.WriteLine("Height:    {0}", rect.Height.Value);
        Console.WriteLine("Perimeter: {0}",
            (rect.Length.Value + rect.Height.Value) * 2);
        Console.WriteLine("Area:      {0}",
            rect.Length.Value * rect.Height.Value);

        return 0;
    }
}

Here is an example of running the program:

Enter the dimensions of the rectangle
Enter the length: 75.82
Enter the height: 55.64

Rectangle Characteristics
Length:    75.82
Height:    55.64
Perimeter: 262.92
Area:      4218.6248
Press any key to continue . . .
 

Previous Copyright © 2006 FunctionX, Inc.