Home

Combinations of Classes

 

Class Nesting

A class can be created inside of another class. A class created inside of another is referred to as nested. To nest a class, simply create it as you would any other. Here is an example of a class called Inside that is nested in a class called Outside:

public class Outside
{
    public class Inside
    {
    }
}

In the same way, you can nest as many classes as you wish in another class and you can nest as many classes inside of other nested classes if you judge it necessary. Just as you would manage any other class so can you exercise control on a nested class. For example, you can declare all necessary fields, properties, or methods in the nested class or in the nesting class. When you create one class inside of another, there is no special programmatic relationship between both classes:  just because a class is nested does not mean that the nested class has immediate access to the members of the nesting class. They are two different classes and they can be used separately as you judge it necessary. 

The name of a nested class is not "visible" outside of the nesting class. To access a nested class outside of the nesting class, you must qualify the name of the nested class anywhere you want to use it. For example, if you want to declare an Inside variable somewhere in the program but outside of Outside, you must qualify its name. 

Because there is no programmatically privileged relationship between a nested class and its "container" class, if you want to access the nested class in the nesting class, you can use its static members. In other words, if you want, you can declare static all members of the nested class that you want to access in the nesting class. If you want to access the nesting class in the nested class, you can go through the static members of the nesting class. To do this, you can declare static all members of the nesting class that you want to access in the nested class.

Instead of static members, if you want to access members of a nested class in the nesting class, you can first declare a variable of the nested class in the nesting class. In the same way, if you want to access members of a nesting class in the nested class, you can first declare a variable of the nesting class in the nested class. 

A Class as a Field

Just like any of the variables we have used so far, you can make a class a member variable of another class. To use a class in your own class, of course you must have that class. You can use one of the classes already available in C# or you can first create your own class. Here is an example of a class:

class Point
{
    public short x;
    public short y;
}

A field is a member variable created from another class instead of a primitive type. To use one class as a member variable of another class, simply declare its variable as you would proceed with any of the member variables we have declared so far. Here is an example:

public class Point
{
    public short x;
    public short y;
}

public class CoordinateSystem
{
    public Point Start;
}

After a class has been declared as a member variable of another class, it can be used regularly. Because the member is a class, declared as a reference, there are some rules you must follow to use it. After declaring the member variable, you must make sure you have allocated memory for it. You must also make sure that the variable is initialized appropriately before it can be used; otherwise you would receive an error when compiling the program.

Returning a Class From a Method

Like a value from a regular type, you can return a class value, called an object, from a method of a class. To do this, you can first declare the method and specify the class as the return type. Here is an example:

public class Point
{
    public short x;
    public short y;
}

public class CoordinateSystem
{
    private Point Start;
    private Point End;

    public Point GetThePoint()
    {
    }
}

After implementing the method, you must return a value that is conform to the class, otherwise you would receive an error when compiling the application. You can proceed by declaring a variable of the class in the body of the method, initializing the variable, and then returning it. Once a method has returned a value of a class, the value can be used as normally as possible.

Passing a Class as Argument

Once a class has been created, it can be used like any other variable. For example, its variable can be passed as argument to a method of another class. When a class is passed as argument, its public members are available to the method that uses it. As done for the arguments of primitive types, you can pass more than one class as argument to a method. Here are different examples:

public class Point
{
    public short x;
    public short y;
}

public class CoordinateSystem
{
    public Point Start;
    public Point End;

    public double DistanceFromOrigin(Point pt)
    {
        double sqr1 = Math.Pow(pt.x, 2);
        double sqr2 = Math.Pow(pt.y, 2);
        double distance = Math.Sqrt(sqr1 + sqr2);
        return distance;
    }

    public double DistanceBetween2Points(Point pt1, Point pt2)
    {
        double sqr1 = Math.Pow(pt2.x - pt1.x, 2);
        double sqr2 = Math.Pow(pt2.y - pt1.y, 2);
        double distance = Math.Sqrt(sqr1 + sqr2);
        return distance;
    }
}

Because classes are always used as references, when passing a class as argument, it is implied to be passed by reference. To reinforce this, you can type the ref keyword to the left of the argument. Here is an example:

public class Point
{
    public short x;
    public short y;
}

public class CoordinateSystem
{
    public Point Start;
    public Point End;

    public double DistanceFromOrigin(ref Point pt)
    {
        double sqr1 = Math.Pow(pt.x, 2);
        double sqr2 = Math.Pow(pt.y, 2);
        double distance = Math.Sqrt(sqr1 + sqr2);
        return distance;
    }

    public double DistanceBetween2Points(ref Point pt1, ref Point pt2)
    {
        double sqr1 = Math.Pow(pt2.x - pt1.x, 2);
        double sqr2 = Math.Pow(pt2.y - pt1.y, 2);
        double distance = Math.Sqrt(sqr1 + sqr2);
        return distance;
    }
}
 

Previous Copyright 2007 FunctionX, Inc. Home