First, you should remember that INSTANCE variables may only be accessed from within the object. The only way to access them from the "outside", is by a parameter or return value of a METHOD or PROPERTY function. Of course, Methods and Properties may also utilize the other data scopes: Global, Local, Static, and Threaded.
In PowerBASIC, the basic unit of code in an object is the METHOD. A METHOD is a block of code, very similar to a user-defined function. Optionally, it can return a value, like a FUNCTION, or merely act as a subroutine, like a SUB. Methods are implemented when you write:
METHOD NAME
[ALIAS "altname"] (var AS type...)
[AS TYPE]
statements...
METHOD = expression
END METHOD
Methods can only be called through an object variable, which is an integral part of the calling syntax. The object variable must be valid, that is, it must contain a valid object reference which was assigned to it with the LET statement. If you attempt to call a method on a null object, you'll likely experience a GPF and a total failure of your program. Methods may be called by writing:
DIM ObjVar
AS MyInterface
LET ObjVar = CLASS "MyClass"
[CALL] objvar.Method1(param)
Note the word CALL is optional. This example shows how to call "Method1" when "Method1" does not return a value. If it did have a return value, use this form instead:
var = ObjVar.Method1(param)
A PROPERTY is a special type of METHOD, which is only designed to GET or SET INSTANCE data in an object. While the work of a PROPERTY could readily be accomplished with a standard METHOD, this distinction is convenient to emphasize the concept of encapsulation of INSTANCE data within an object. There are two forms of PROPERTY procedures, PROPERTY GET and PROPERTY SET. As implied by the names, the first form is used to retrieve a data value from the object, while the second form is used to assign a value. Properties are implemented:
PROPERTY GET NAME
[ALIAS "altname"] (BYVAL
var AS type...)
[AS TYPE]
[statements]
PROPERTY = expression
END PROPERTY
PROPERTY SET NAME [ALIAS "altname"] (BYVAL var
AS type...)
[statements]
variable = value
END PROPERTY
When you use PROPERTY SET, the last (or only) parameter is used to pass the value to be assigned. A PROPERTY may be considered "Read-Only" or "Write-Only" by simply omitting one of the definitions. However, if both GET and SET forms are defined for a particular Property, parameters and the property must be identical in both forms, and they must be paired. That is, the PROPERTY SET must immediately follow the PROPERTY GET. It's important to note that all PROPERTY parameters must be declared as BYVAL.
Properties can only be called through an object variable, which is an integral part of the calling syntax. The object variable must be valid, that is, it must contain a valid object reference which was assigned to it with the LET statement.
You can access a PROPERTY GET with:
DIM ObjVar
AS MyInterface
LET ObjVar = CLASS "MyClass"
var = ObjVar.Prop1(param)
You can access a PROPERTY SET with:
DIM ObjVar
AS MyInterface
LET ObjVar = CLASS "MyClass"
[CALL] ObjVar.Prop1(param) = expr
Note that the choice of Property procedure is syntax directed. In other words, depending upon the way you use the name, PowerBASIC will automatically decide whether the GET or SET PROPERTY should be called.
In every Method and Property, PowerBASIC automatically defines a pseudo-variable named ME, which is treated as a reference to the current object. Using ME, it's possible to call any other Method or Property which is a member of the class:
var = ME.Method1(param)
Methods and Properties may be declared (using AS type...) to return a string, any of the numeric types, a specific class of object variable (AS MyInterface), a Variant, or a user defined Type.
See Also
What is a Compound Object Reference?