JAL Computing

C++COMProgramming .NET Mac Palm CPP/CLI Hobbies

 

Home
Up

In this code sample, HelloWorld2.js, we pass a null string to the class constructor. This project demonstrates the potential danger of accessing a property, length, of a null string. Note the use of the short circuited logical AND to avoid a null pointer run time error.

Source Code

import System;
                                          
// demonstrates setting member property "message" in the constructor
class HelloWorld {
	private var message : String = "Null or Empty String";       

    	// constructor
	function HelloWorld(inString : String) {
        		// short circuited logical AND to avoid run time null pointer error
        		if (inString != null && inString.length > 0) { 
            			message= inString;
        		}
	}

	function SayHello() {
		Console.WriteLine(message);	
	}
}
// create an instance of the HelloWorld class                                     
var hw= new HelloWorld(null);
// call a method on the object
hw.SayHello();

Learn More About "this"

Here is the more "eloquent" version that demonstrates the use of the key word "this". Notice that the constructor variable "inString" has been replace with the constructor variable "message." The program now contains two variables named "message", the member variable and a local constructor variable. Within the constructor, one can refer to the member variable as this.message.

import System;
                                          
// demonstrates using "this" key word to avoid name collision
// between member variable "message" and constructor variable "message"                                                 
class HelloWorld {
    	private var message : String = "Null or Empty String";       

    	// constructor
	function HelloWorld(message : String) {
        		// short circuited logical AND to avoid run time null pointer error
        		if (message != null && message.length > 0) { 
            			this.message= message;  // <-- demonstrates the use of "this"
        		}
	}

	function SayHello() {
		Console.WriteLine(message);	
	}
}
                                     
var hw= new HelloWorld("Hello");
hw.SayHello();

Learn More About Short Circuited Logical AND

The logical AND operator, &&, returns true if both the left and right hand operand evaluates to boolean true. However, the evaluation is "short circuited" if the the first operand evaluates to false. Since the result of the logical AND operation is known if the left sided operand is false, there is no need to actually evaluate the right hand operand. For efficiency, the right hand operand is not evaluated if the left hand operand returns false. In other words, the evaluation is optimized at compile time. We can use this "short circuited" behavior to avoid a run time null pointer exception. Calling a method or accessing a property on a null object will throw an exception at runtime, unexpectedly crashing the application with the message "Object reference not set to an instance of an object." Here is the snippet code in our project that demonstrates the use of logical AND:

if (message != null && message.length > 0) {
    this.message= message;
}

If the user passes a null object to this method, the evaluation is short circuited and the right hand operand, message.length, is not evaluated or executed. Pretty tricky.

Learn More About Constructors

If we declare a function with the same name as the class, it is compiled as a constructor for the class. The constructor does just what it sounds like it should do, it constructs an object (an instance) of the class. The appropriate constructor is called when an object is created using the key word new.

var hw= new HelloWorld(null);

We usually place initialization code in the constructor. If we don't declare a constructor, then the compiler will create an empty one, the so called default constructor, that does not take any arguments.
Previous Next
Send mail to [email protected] with questions or comments about this web site. Copyright © 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 © 
Last modified: 08/04/09
Hosted by www.Geocities.ws

1