My MathClass example shows that MathClass
is defined at PROGRAM level, ie: it has GLOBAL scope, it will be visible to all procedures that follow in the main program and standard member modules.
You can use this GLOBAL instance inside any procedure without redeclaring it thus …
myFirstProc Procedure
a LONG
b LONG
x LONG
Code
a = 1
b = 2
x = MathClass.Add2(a,b)
but it is more common to define a CLASS type, think of this this as what a CLASS should be “like” rather than what it “is”.
So, let’s change the previous example into a TYPE, we’ll rename it slightly to make it more obvious, and append the ,TYPE attribute to its declaration:
PROGRAM
MathClassType CLASS,TYPE
Add2 Procedure(LONG in1,LONG in2),LONG
Add3 Procedure(LONG in1,LONG in2,LONG In3),LONG
.
MAP
END
CODE
!-- Main program code called here
MathClassType.Add2 PROCEDURE(LONG in1,LONG in2)
Code
RETURN(in1 + in2)
MathClassType.Add3 Procedure(LONG in1,LONG in2,LONG In3)
Code
RETURN(SELF.Add2(in1,in2) + in3)
We have redefined the class and still have the methods that do something, namely add 2 or 3 numbers together, you can now declare an individual instance of this class in a procedure of your own and use it independently of other instances in other procedures, ie: it is safe, the individual instances won’t interfere with each other.
AnotherProcedure Procedure
myAdder MathClassType
a LONG
b LONG
x LONG
Code
a = 1
b = 2
x = myAdder.Add2(a,b)
Hope that helps a bit more.
A good place to start when writing your own class is to try and find some code that you’ve used several times, have you cut and pasted between apps or procedures to reuse it, if so, that’s a prime candidate for making into a CLASS … code it once, use it many times.