Home

Combining of Classes

 

Returning a Class

 

A Class as a Member Variable

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 must first have a class. You can use one of the classes already available or you can first create your own class. Here is an example of a class:

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

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

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

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

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

<%@ 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:

Point

Passing Classes as Arguments

As done for the arguments of primitive types, you can pass more than one class as argument to a method. Here is an example:

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

<%@ 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:

Point

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:

<%@ 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:

House

 
 
 
 

Passing a Class by Reference

Consider the following code:

<%@ 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:

Square

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:

<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

An instance of a class can be passed as an argument to one of its own methods. To do this, you primarily pass the argument as if it were any class. 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.

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

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.

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-2015, FunctionX, Inc. Next