JAL Computing

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

 

Home
Up

In this final console project, we demonstrate .NET's support for "methods as properties" with the key words set and get. Most published sample projects use an upper case convention for "properties" declared with set and get such as MyProperty. The key words get and set allow us to write setter and getter functions and access these functions with the convenient dot syntax of a property such as myObject.MyProperty= 1; This project also demonstrates the use of the key word const to create a reference to an immutable String.

Source Code

import System;

// demonstrates support for properties with key words set and get
class HelloWorld {
    // ERROR string is declared a constant and is immutable
   
private const ERROR : String = "Error: Null or Empty String";
    // init the message variable here for safety
    private var message : String = ERROR;

    // get function
   
function get Message() : String {
        return message;
    }


    // set function
    // ASSERT message is not null or empty string
   
function set Message(message : String) {
        if (message != null && message.length > 0) {
            this.message= message;
        }
        else {
            this.message= ERROR;
        }
    }


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

    function SayHello() {
        Console.WriteLine(message);
    }
}  // end_class

var hw= new HelloWorld(null); // invalid null message
hw.SayHello();
hw.Message= "Hello World"; // set valid message using set
hw.SayHello();
hw.Message= ""; // invalid empty message
Console.WriteLine("Using Get: "+
hw.Message); // use get

Learn More About "get", "set"

It may help to look at getter and setter methods declared with the key words get and set as "methods that appear as properties", or methods as properties for short. Other languages also support this concept. For instance, ATL C++ dlls support the concept of get and put methods that can be accessed from a scripting language using the dot notation of a property. Any error checking can then be implemented in the set method. In this project, we substitute a error message if the user passed string  is a null object or if the string is empty.

Learn More About "const"

Often we want to refer to a constant value within a class. In this sample project, we have an error message that we want to define at compile time.

private const ERROR : String = "Error: Null or Empty String";

 By convention, in many languages, references to constant values are in all caps like MAX_SIZE. We declare a constant value using the key word const.  In JScript, a constant value can be type less (undeclared type) or typed.  (Note that declaring an array of values const does not protect the values in the array from being changed.)

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