Wednesday, March 24, 2010

Declaring and Using Structs

To declare a struct, use the keyword like this example:
struct MyStruct
{
    //The struct members
}
We can modify the declaration with any valid combination of access modifiers or the new keyword. When we use a struct, keep in mind the following points:
•    Unlike classes, the fields of a struct cannot be initialized. That means that the following statements will generate error messages:
struct MyStruct
{
int x = 0; //error
int y = 0; //error
}
•    A struct can use one or more constructors, but we cannot declare a default constructor (parameter less constructor) for a struct. Thus, the following constructor is invalid:
struct MyStruct
{
    MyStruct()    //error
    {
        //….
    }
}
The compiler always provides a default constructor to initialize the members of a struct to their default values.
•    Like classes, we can use the new operator to create struct objects as shown in the following example:
MyStruct ms = new MyStruct();
Unlike classes, we can declare an object without the new operator, like this:
MyStruct ms;
In the latter case, we must initialize the struct fields before using it.
As mentioned in the preceding chapters, a struct cannot be inherited or inherit from another type but it can implement one or more interfaces. For example:
struct MyStruct: IMyInterface
{
    //….
}
Although structs cannot inherit other types, the struct type itself descends from the Object class.
The following example demonstrates the Color struct, which contains three fields (r, g, and b) representing the three basic colors (red, green, and blue). The program uses a constructor to create two objects with different colors.
//Example 7-1.cs
//struct example
using System;
public struct Color
{
    //Fields:
    private int r;
    private int g;
    private int b;

    //Constructor:
    public Color(int r, int g, int b)
    {
        this.r = r;
        this.g = g;
        this.b = b;
    }
    //Overriding the method ToString():
    public override string ToString()
    {
        return (String.Format(“Red = {0}, Green = {1}, Blue = {2}”, r, g, b));
    }
   
}
class MyClass
{
    static void Main()
    {
        //Declare objects:
        Color c1 = new Color();     //uses the default values
        Color c2 = new Color(100,100,0);
        //Display objects:
        Console.WriteLine(“The first object:”);
        Console.WriteLine(“The colors are: {0}”, c1);
        Console.WriteLine(“The second object: “);
        Console.WriteLine(“The colors are: {0}”, c2);
    }
}
In the above example, two struct instances, c1 and c2, were used. The first one uses a default constructor; therefore, all of its fields contain the value zero. The second instance is constructed using the parameters 100,100 and 0. These values are assigned to the fields r, g, and b.
The objects are displayed directly by using the statement:
Console.WriteLine(“The colors are: {0}”, c1);
This requires overriding the ToString method to display objects in the appropriate format.
In the following example, the three properties R,G, and B are used to access the private fields r, g, and b. This makes it possible to access the fields directly through the properties, which is an alternative to using constructors.
//Example 7-2.cs
//Using properties with structs
using System;
public struct Color
{
    //Fields:
    private int r;
    private int g;
    private int b;
    //Properties:
    public int R
    {
        get { return r; }
        set { r = value; }
    }
    public int G
    {
        get { return g; }
        set { r = value; }
    }
    public int G
    {
        get { return g; }
        set { g = value; }
    }
    public int B
    {
        get { return b; }
        set { b = value; }
    }
    //Override ToString():
    public override string ToString()
    {
        return (String.Format(“Red = {0}, Green = {1}, Blue = {2}”, R, G, B));
    }
}
class MyClass
{
    static void Main()
    {
        Color c1 = new Color();
        Console.WriteLine(“The colors are: {0}”, c1);
        c1.R = 100;
        c1.G = 100;
        c1.B = 0;
        Console.WriteLine(“The colors are: {0}”, c1);
    }
}
Notice in the above example that we used one object created with the default constructor. Then we used the color properties to assign color values to the corresponding fields:
    c1.R = 100;
    c1.G = 100;
    c1.B = 0;
The object was displayed twice – once with the default values and once after assigning the values to the properties. We can, of course, use a constructor with three parameters to achieve the same result.

No comments:

Post a Comment