ASP.NET (Newer Series)

Combinations of Classes

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…

6 min read1,282 words16 code samples

Classes Combinations#

Class Nesting#

Classes can be defined within the scope of another class, a technique known as nesting. To create a nested class, you define it just like a standard class but within the body of another. For example, here is an Inside class defined within an Outside class:

C#
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. Here is an example:

C#
public class Outside
{
    public class Inside
    {
	public Inside()
	{
	    Response.Write(" -= Inside =-");
	}
    }

    public Outside()
    {
	Response.Write(" =- Outside -=");
    }
}

A Class as a Field#

Just like any of the variables we have used so far, you can make a class or 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:

C#
public class Point
{
    internal short x;
    internal short y;
}

A field can also be an instance of another class rather than a simple primitive type. To include one class as a member of another, you declare a variable for it just like you would for any other data member. Here is an example of this approach:

C#
public class Point
{
    internal short x;
    internal 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 or Passing a Class#

Returning a Class From a Method#

Like a value from a regular type, you can return a class value 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:

C#
public class Point
{
    internal short x;
    internal 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 running 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. Here is an example:

C#
public class Point
{
    internal short x;
    internal short y;
}

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

    public Point GetThePoint()
    {
        Point pt = new Point();

        return pt;
    }
}

When a method provides an object as its return value, that object can be utilized throughout your program in the same way as any other instance.

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. 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.

Involving a Class and its Own Methods#

Passing a Class as its Own Argument#

An instance of a class can be passed as an argument to one of its own methods (if you have programmed in C++, an example of this implementation is the copy constructor; although you can legitimately create a copy constructor in C#, it does not have the exact same concept as in C++, probably because C# has the Equals() method). To do this, you primarily pass the argument as if it were any class. Here is an example:

C#
public class Point
{
    internal int x;
    internal int y;

    public void Equivalent(Point Same)
    {
    }
}

Then, in the body of the method, do whatever you want. You can, or you may not, use the argument. Still, if you decide to use the argument, know that all of the other members of the class are available through the argument. Probably the simplest way to use the argument is the assign each of of its values to the equivalent member of the class. Here is an example:

C#
public class Point
{
    internal int x;
    internal int y;

    public void Equivalent(Point Same)
    {
        this.x = Same.x;
        this.y = Same.y;
    }
}

When calling the method, make sure you pass an instance of the class to it. You can first create and define the class, then pass it. Here is an example:

C#
public class Point
{
    internal int x;
    internal int y;

    public void Equivalent(Point Same)
    {
        this.x = Same.x;
        this.y = Same.y;
    }
}

Instead of first declaring a variable of the class and initializing it, you can create an instance of the class in the parentheses of the calling method. To do this, you may need a constructor that can specify the values of the fields of the class so the argument can be rightfully initialized. Here is an example:

C#
public class Point
{
    internal int x;
    internal int y;

    public Point()
    {
    }

    public Point(int XCoord, int YCoord)
    {
        this.x = XCoord;
        this.y = YCoord;
    }

    public void Equivalent(Point Same)
    {
        this.x = Same.x;
        this.y = Same.y;
    }
}

Instead of a formal method, you can use a constructor of the class to pass an instance of the same class. Then, in the constructor, use the argument as you see fit, knowing that all the members of the class are available. Here is an example:

C#
public class Point
{
    internal int x;
    internal int y;

    public Point()
    {
    }

    public Point(int XCoord, int YCoord)
    {
        this.x = XCoord;
        this.y = YCoord;
    }

    public Point(Point Same)
    {
        this.x = Same.x;
        this.y = Same.y;
    }
}

Obviously the purpose of passing a class to one of its own methods is not to find its equivalent. The C# language can also take care of that. Instead, you can create a method that takes an instance of the same class but modifies that instance. For example, for our Point class, we may want to create a new point that is distanced by one unit from the current Point object. Here is an example of doing that:

C#
public class Point
{
    internal int x;
    internal int y;

    public Point()
    {
    }

    public Point(int XCoord, int YCoord)
    {
        this.x = XCoord;
        this.y = YCoord;
    }

    public void Equivalent(Point Same)
    {
        this.x = Same.x;
        this.y = Same.y;
    }

    public void CreatePointOneUnitAway(Point AddUnit)
    {
        this.x = AddUnit.x + 1;
        this.y = AddUnit.y + 1;
    }
}

Returning a Class From its Own Method#

You can create a method in a class that returns an instance of the class. To start, on the left side of the method, enter the name of the class. Here is an example:

C#
public class Point
{
    public Point MethodName()
    {
    }
}

There are various ways you can deal with the method. If you want to return a new value of the class, you can declare an instance of the class, initialize it, and then return it. Here is an example:

C#
public class Point
{
    internal int x;
    internal int y;

    public Point()
    {
    }

    public Point(int XCoord, int YCoord)
    {
        this.x = XCoord;
        this.y = YCoord;
    }

    public Point(Point Same)
    {
        this.x = Same.x;
        this.x = Same.x;
    }

    public Point AdvanceBy5()
    {
        Point Some = new Point();
        Some.x = 5;
        Some.y = 5;
        return Some;
    }
}

Alternatively, you can declare an instance of the class, use the current values of the class combined with those of the instance to get new values, and then return the instance. Here is an example:

C#
public class Point
{
    internal int x;
    internal int y;

    public Point()
    {
    }

    public Point(int XCoord, int YCoord)
    {
        this.x = XCoord;
        this.y = YCoord;
    }

    public Point(Point Same)
    {
        this.x = Same.x;
        this.x = Same.x;
    }

    public Point AdvanceBy5()
    {
        Point Some = new Point();
        Some.x = this.x + 5;
        Some.y = this.y + 5;
        return Some;
    }
}

Remember that, to call the method, you will need to declare an instance of the class from where you are calling the method. The second type of implementation consists of modifying the instance of the class that is calling the method. For example, you can add values to its fields or you can perform any other operation you want on the members of the calling instance. is an example:

C#
public class Point
{
    internal int x;
    internal int y;

    public Point()
    {
    }

    public Point(int XCoord, int YCoord)
    {
        this.x = XCoord;
        this.y = YCoord;
    }

    public Point(Point Same)
    {
        this.x = Same.x;
        this.x = Same.x;
    }

    // This method adds 1 to each field of the class
    // to get a new point away North-East of the current point
    public Point CreatePointOneUnitAway()
    {
        this.x = this.x + 1;
        this.y = this.y + 1;

        return this;
    }
}

As we have learned now, you can create a method that takes an argument that is the same type as its parent class. In the method, you can access any member of the class, including calling the other methods of the class. Previous Copyright © 2008-2009 C# Key Next