- The Class Hierarchy
- Creating a New Class
- Declaration and Instantiation
- Constructors and Destructors
- Garbage Collection
- Object Operators
- Adding and Overriding Methods
- Calling the Overridden Method
- Access Scope: Public, Private, Protected
- Setting Properties with Methods
- Default and Optional Parameters
- Declaring Variables Static and Const
- Revisiting the StringParser Module
- Example: Creating a Properties Class
- Data-Oriented Classes and Visual Basic Data Types
- Advanced Techniques
Constructors and Destructors
When operators were introduced, I said that they were basically functions. They are given input in the form of operands; the operator causes a calculation to be performed, and the results are returned. Because New is an operator, that must mean that it's a function, too. As you can see from this example, the value returned by the New operator is an instance of Alpha, which is assigned to the variable a. The reason I followed Alpha with parentheses when instantiating a is so that it will be more clear that it is acting as a function. You are, in fact, invoking a function when you use the New operator. Your ability to refer to the class name alone, without explicitly typing out the method, is a matter of convenience.
REALbasic (and other object-oriented languages) gives this method a unique name: Constructor. It is the Constructor method that is being called when you use the New operator. In many cases, the default Constructor is all you need—all it does is instantiate the object, setting aside space for properties and things like that, but not setting their value. You can add to this by implementing your own Constructor.
Right now, our Alpha class returns the string "Alpha" when we call the function getName(). Suppose that we want to be able to decide what value this function will return when we instantiate the object. The way to do this is to create a property, which will hold the value that the getName() function will return, and we will add a new Constructor method that will allow us to pass the string we want returned when we first instantiate the object.
The first step is to create the property, like you did when creating a module. Set the access scope of this property to Private and give it the name "Name", and a data type of string.
Next, add the method called "Constructor" (Constructors are always public). Do not specify a return value. Implement it as a subroutine (it knows automatically to return an instance of the class it's a member of).
Sub Constructor(aName as String) me.Name = aName End
Finally, we need to update our getName() method.
Function getName() as String return me.Name End
You have now implemented a new Constructor that takes a string as a parameter and assigns the value of that string to the property Name. Now, if you try to instantiate this class like you did before, you'll get an error. For you to instantiate it, you need to pass a string to the Constructor.
Dim a as Alpha Dim s as String a = New Alpha("Blue") s = a.getName() // s = "Blue"
New in REALbasic 2005, you can set the values of properties implicitly when the class is instantiated, rather than explicitly as I did in this example. This is done by filling in the values in the Code Editor as shown in Figure 3.4.
Figure 3.4 Set values for class properties in the Code Editor.
In earlier versions of REALbasic, the Constructor method was named after the name of the class itself. So in this case, you can name the constructor "Alpha" and it would be called when the class is instantiated.
There is also a Destructor method that's called when the object is being purged from memory. It can be declared by calling it Destructor or by using the object name preceded by the tilde (~) character. For the Alpha class, you would name it "~Alpha". The default is to use the terms Constructor and Destructor, and I would recommend sticking with that—I only share the other method in case you encounter it in preexisting code.