Home

Class Construction and Destruction

 

Using a Method to Initialize an Object

 

Introduction

Before using a variable of a class, you can assign a value for some or each member variable. This is referred to as initializing a class. Instead of initializing each individual field of a class, you can create a method that would take care of this. When defining the method, you can pass as argument each field that would need to be initialized. The method can be started as follows:

<script runat="server">
public class House
{
    public long PropertyNumber;

    public void Initialize(long nbr)
    {
    }
}
</script>

Overloading the Method

As mentioned above, you can pass more than one argument, using a different argument for each field that must be initialized. Here is an example:

<script runat="server">
public class House
{
    public long PropertyNumber;
    public string PropertyType;
    public uint Bedrooms;
    public double Value;

    public void Initialize(long nbr, string type, uing beds, double value)
    {
    }
}
</script>

When implementing the method, you can assign the necessary value to each argument to the corresponding member variable of the class. Here are examples:

<script runat="server">
public class House
{
    public long PropertyNumber;
    public string PropertyType;
    public uint Bedrooms;
    public double Value;

    public void Initialize(long nbr, string type, uint beds, double value)
    {
	PropertyNumber = nbr;
	PropertyType = type;
	Bedrooms = beds;
        Value = value;
    }
}
</script>

After defining this method, you can call it to initialize a variable declared from the class. Here is an example:

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

<html>
<head>

<script runat="server">
public class House
{
    public long PropertyNumber;
    public string PropertyType;
    public uint Bedrooms;
    public double Value;

    public void Initialize(long nbr, string type, uint beds, double value)
    {
	PropertyNumber = nbr;
	PropertyType = type;
	Bedrooms = beds;
        Value = value;
    }

    public string Describe()
    {
	string description;

    	description = "<pre>=//= Altair Realty =//=<br />" +
                       "Properties Inventory<br />" +
                       "Property #:     " + PropertyNumber + "<br />" + 
                       "Property Type:  " + PropertyType + "<br />" + 
                       "Bedrooms:       " + Bedrooms + "<br />" + 
                       "Market Value:   " + Value + "</pre>";

	return description;
    }
}
</script>

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

<%
    House property = new House();
    property.Initialize(240085, "Townhouse", 3, 465895);
%>

<% Response.Write(property.Describe()); %>

</body>
</html>

This would produce:

House

Constructors

 

Introduction

A constructor is a special method that is created when the object comes to life. This particular method holds the same name as the class and it initializes the object whenever that object is created.

Default Constructor

When you create a class, if you don't declare a constructor, the compiler creates one for you. This compiler-created constructor is called the default constructor.

To create a constructor, declare a method that holds the same name as the class. The method must not return any value.

Here is an example:

<script runat="server">
public class House
{
    House()
    {
    }
}
</script>

When you declare an instance of the class, whether you use that object or not, a constructor for the object is created. When an instance of a class has been declared, the default constructor is called, whether the object is used or not.

A Constructor that Initializes

A constructor can be used to initialize the member variables of a class. To implement a default constructor, you can just initialize the desired members of the class. Here is an example:

<script runat="server">
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;
    }
}
</script>

After using the default constructor, when you declare a variable of the class, that default constructor is called. You can then access the class' fields and use them as you see fit. For example, you can display their values:

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

<html>
<head>

<script runat="server">
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 string Describe()
    {
	string description;

    	description = "<pre>=//= Altair Realty =//=<br />" +
                      "Properties Inventory<br />" +
                      "Property #:     " + PropertyNumber + "<br />" + 
                      "Property Type:  " + PropertyType + "<br />" + 
                      "Bedrooms:       " + Bedrooms + "<br />" + 
                      "Market Value:   " + Value + "</pre>";

	return description;
    }
}
</script>

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

<%
    House property = new House();
%>

<% Response.Write(property.Describe()); %>

</body>
</html>

This would produce:

House   

 
 
 
 

A Constructor that Initializes

As mentioned already, a default constructor is always automatically created if you don't create any constructor. Instead of using the default constructor, you can create your own constructor and pass it at least one argument. Here is an example:

<script runat="server">
public class House
{
    public long PropertyNumber;

    public House(long nbr)
    {
    }
}
</script>

When implementing the constructor, you can assign the argument to its corresponding field. Here is an example:

<script runat="server">
public class House
{
    public long PropertyNumber;

    public House(long nbr)
    {
	PropertyNumber = nbr;
    }
}
</script>

In the same way, you can use that constructor to initialize the other fields with the default values. When you declare a variable from the class, you can use this constructor to initialize the object. Here is an example:

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

<html>
<head>

<script runat="server">
public class House
{
    public long PropertyNumber;
    public string PropertyType;
    public uint Bedrooms;
    public double Value;

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

    public string Describe()
    {
	string description;

    	description = "<pre>=//= Altair Realty =//=<br />" +
                       "Properties Inventory<br />" +
                       "Property #:     " + PropertyNumber + "<br />" + 
                       "Property Type:  " + PropertyType + "<br />" + 
                       "Bedrooms:       " + Bedrooms + "<br />" + 
                       "Market Value:   " + Value + "</pre>";

	return description;
    }
}
</script>

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

<%
    House property = new House(290375);
%>

<% Response.Write(property.Describe()); %>

</body>
</html>

Constructor Overloading

The default constructor is the favorite place to provide default values to the members of a class. Besides the default constructor, you can add as many constructors as you judge necessary. This also means that constructor can be overloaded.

The rules to overload a constructor are the same as for methods: the constructors must have either different types of arguments or different numbers of arguments. Here is an example:

<script runat="server">
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 House(long nbr)
    {
	PropertyNumber = nbr;
	PropertyType = "Condominium";
	Bedrooms = 1;
        Value = 0;
    }

    public House(long nbr, string type, uint beds, double value)
    {
	PropertyNumber = nbr;
	PropertyType = type;
	Bedrooms = beds;
        Value = value;
    }
}
</script>

After creating the constructors, when declaring a variable from the class, you can use the constructor that suits you at one particular moment to initialize an object.

If you create a class with only one constructor, when declaring an instance of the class, you must use that constructor: you cannot use the default constructor that doesn't take an argument. If you create a class with only one constructor and that constructor has at least one argument, the default constructor would not be available anymore. If you want to access a default constructor of an object, you have two alternatives:

  • If you don't create any constructor at all on a class, the default constructor would always be available whenever you invoke that class
  • If you create at least one constructor and supply at least one argument to that constructor, you must explicitly create a default constructor for your class.

The Destructor of a Class

As opposed to a constructor, a destructor is called when a program has finished using an object. A destructor does the cleaning behind the scenes. Unlike the constructor, the destructor cannot be overloaded. This means that, if you decide to create a destructor, you can have only one. Like the default constructor, a destructor also has the same name as its class. This time, the name of the destructor starts with a tilde "~".

To create a destructor, type ~ followed by the name of the class. Here is an example:

<script runat="server">
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 ~House()
    {
    }
}
</script>
 
 
   
 

Previous Copyright © 2009-2016, FunctionX, Inc. Next