- The Class Hierarchy
- Creating a New Class
- Declaration and Instantiation
- Constructors and Destructors
- Garbage Collection
- Inheritance
- Object Operators
- Adding and Overriding Methods
- Calling the Overridden Method
- Overloading
- Casting
- Oddities
- Encapsulation
- 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
Declaration and Instantiation
When you build a house, you start with the blueprints, but it's not a house until you build the walls, roof, and so on, and eventually people move in. I said earlier that a class is like a blueprint. Because the Alpha class is a blueprint, it needs to be built in order to be used. This is called instantiation, or creating an instance of the Alpha class. An instance of a class is called an object.
This is one area where classes differ from modules. You do not have to instantiate a module to have access to the methods and properties of the module. Instantiating an object is a two-part process. First, the object must be declared, just like variables are declared. The only difference is that the type of this variable is going to be the class name rather than one of REALbasic's intrinsic data types. The second step is the actual instantiation, the part where the "house is built." When you build a house, you need boards and nails. When you declare an object, you are setting aside space for the constants, properties, and methods of the class, and when you instantiate an object, you are filling up the space set aside with the constants, properties, and methods of the class. Again, it's like building a house—first the rooms are made, and then people move in to live in them.
Dim a as Alpha Dim s as String a = New Alpha() s = a.getName() // s equals "Alpha"
The variable is declared with a Dim statement, and it is instantiated using the New operator. After the class is instantiated and assigned to the variable a, you can use dot notation to access the members of the class, much like you can access the members of a module. In this example, the method getName() is called and the value returned is assigned to the string s.
Always eager to provide help to those like me who have an aversion to extra typing, the engineers at REALbasic have provided a handy shortcut that combines these two steps into one (this works with data types, too).
Dim a as New Alpha() Dim s as String s = a.getName() //s equals "Alpha"