ASP.NET

Combinations of Classes

Just like a variable of a primitive type (int, byte, double, etc), you can make a class or a structure a member variable of another class. Of course, you…

3 min read600 words11 code samples

Returning a Class#

A Class as a Member Variable#

Similar to variables representing primitive types like int, byte, or double, you can define a class or a structure as a member within another class.

C#
<script runat="server">

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

</script>

To use one class as a member variable of another class, simply declare its variable as you would proceed for a member variable of a primitive type. Here is an example:

C#
<script runat="server">

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

public class CoordinateSystem
{
    Point Start;
}

</script>

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. For example, before using the variable, you must make sure you have allocated memory for it.

Returning a Class From a Method#

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

C#
<script runat="server">

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

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

    public Point GetPoint()
    {
    }
}

</script>

After implementing the method, you must return a value of the class's type. Here is an example:

C#
<script runat="server">

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

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

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

        pt.x = 6;
        pt.y = 4;

        return pt;
    }
}

</script>

Once a method has returned a value of a class, the value can be used as normally as possible.

Passing a Class as Argument#

Introduction#

You can pass a class as argument to a method. When a class is passed as argument, its public members are available to the method that uses it. Here is an example:

C#
<script runat="server">

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

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

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

        pt.x = 6;
        pt.y = 4;

        return pt;
    }

    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;
    }
}

</script>

When calling the method, make sure you pass a variable of the type of the class. Here is an example:

C#
<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">

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

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

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

        pt.x = 6;
        pt.y = 4;

        return pt;
    }

    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;
    }
}

</script>

<title>Exercise</title>
</head>
<body>

<%
    CoordinateSystem coord = new CoordinateSystem();
    Point p = coord.GetPoint();
    double d = coord.DistanceFromOrigin(p);
 
    Response.Write("<pre>Coordinate System<br />");
    Response.Write("Point: P(" + p.x + ", " + p.y + ")<br />");
    Response.Write("Distance From Origin: " + d + "</pre>");
%>
</body>
</html>

This would produce:

Passing Classes as Arguments#

Just as with primitive type parameters, it is possible to pass multiple classes as arguments into a single method.

C#
<script runat="server">

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

public class CoordinateSystem
{
    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;
    }
}

</script>

When calling the method, remember to pass the right number and right type(s) of argument(s). Here is an example:

C#
<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">

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

    public Point(short X, short Y)
    {
	x = X;
	y = Y;
    }
}

public class CoordinateSystem
{
    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;
    }
}

</script>

<title>Exercise</title>
</head>
<body>

<%
    CoordinateSystem coord = new CoordinateSystem();
    Point p = new Point(5,2);
    Point q = new Point(-3, 1);
    double d = coord.DistanceBetween2Points(p, q);
 
    Response.Write("<pre>Coordinate System<br />");
    Response.Write("Point: P(" + p.x + ", " + p.y + ")<br />");
    Response.Write("Point: Q(" + q.x + ", " + q.y + ")<br />");
    Response.Write("Distance between them: " + d + "</pre>");
%>
</body>
</html>

This would produce:

In our example, we passed two class arguments that are of the same type. You can pass arguments that are of different types. Then, in the method, use the arguments as you see fit. Here is an example:

C#
<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">

public class Rectangle
{
    public double Length;
    public double Width;

    public Rectangle()
    {
        Length = 0.00;
	Width  = 0.00;
    }

    public Rectangle(double len, double wid)
    {
        Length = len;
	Width  = wid;
    }

    public double Area()
    {
        return Length * Width;
    }
}

public class House
{
    public long PropertyNumber;
    public string PropertyType;
    public uint Bedrooms;
    public double Value;

    public House()
    {
	PropertyNumber = 0;
	PropertyType = "Condominium";
	Bedrooms = 1;
        Value = 0;
    }
}

public class Property
{
    public Rectangle DefineArea()
    {
	Rectangle recto = new Rectangle();

	recto.Length = 22.50;
	recto.Width = 16.75;

	return recto;
    }

    public House CreateHouse()
    {
	House town = new House();

	town.PropertyNumber = 927419;
	town.PropertyType = "Townhouse";
	town.Bedrooms = 3;
        town.Value = 550250;

	return town;
    }

    public string Describe(Rectangle rect, House prop)
    {
        return "<pre>=//= Altair Realty =//=<br />" +
               "Property Description<br />" +
               "Property #:    " + prop.PropertyNumber + "<br />" + 
               "Property Type: " + prop.PropertyType + "<br />" + 
               "Bedrooms:      " + prop.Bedrooms + "<br />" + 
               "Length:        " + rect.Length + "<br />" +
               "Width:         " + rect.Width + "<br />" +
               "Area:          " + rect.Area() + "<br />" +
               "Market Value:  " + prop.Value + "</pre>";
    }
}

</script>

<title>Exercise</title>
</head>
<body>

<%
    Property prop = new Property();

    House h = prop.CreateHouse();
    Rectangle r = prop.DefineArea();
 
    Response.Write(prop.Describe(r, h));
%>
</body>
</html>

This would produce:

Passing a Class by Reference#

Consider the following code:

C#
<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">

public class Square
{
    public double Radius;

    public Square()
    {
        Radius = 0.00;
    }

    public Square(double radius)
    {
        Radius = radius;
    }

    public double Perimeter()
    {
        return Radius * 4;
    }

    public double Area()
    {
        return Radius * Radius;
    }
}

public class Geometry
{
    public void Create(Square sq)
    {
        sq.Radius = 38.62;
    }

    public string Show(Square sq)
    {
        return "<pre>Square Characteristics<br />" +
               "Radius:    " + sq.Radius + "<br />" +
               "Perimeter: " + sq.Perimeter() + "<br />" +
               "Area:      " + sq.Area() + "</pre>";
    }
}

</script>

<title>Exercise</title>
</head>
<body>

<%
    Geometry figure = new Geometry();
    Square sqr = new Square();

    figure.Create(sqr);
 
    Response.Write(figure.Show(sqr));
%>
</body>
</html>

This would produce:

Notice that the method that receives a class as argument gives back an object that has been modified. Because a class is always used as a reference, when passing one 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:

C#
<script runat="server">

public class Square
{
    public double Radius;

    public Square()
    {
        Radius = 0.00;
    }

    public Square(double radius)
    {
        Radius = radius;
    }

    public double Perimeter()
    {
        return Radius * 4;
    }

    public double Area()
    {
        return Radius * Radius;
    }
}

public class Geometry
{
    public void Create(ref Square sq)
    {
        sq.Radius = 38.62;
    }

    public string Show(Square sq)
    {
        return "<pre>Square Characteristics<br />" +
               "Radius:    " + sq.Radius + "<br />" +
               "Perimeter: " + sq.Perimeter() + "<br />" +
               "Area:      " + sq.Area() + "</pre>";
    }
}

</script>

In the same way, if passing more than one class as arguments, if you want, you can type the ref keyword to the left of each argument.

Involving a Class and its Own Methods#

Passing a Class as its Own Argument#

A class instance can be passed into its own methods as an argument. This is primarily achieved using the this keyword, which refers to the current class within the method's logic.

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. 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. 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. Previous Copyright © 2009-2011 C# Key Next