What does a Class look like?

Here is the PowerBASIC source code for a very simple class.  It has just one interface and one instance variable.

CLASS MyClass
  INSTANCE Counter AS LONG
  INTERFACE MyInterface
    INHERIT IUNKNOWN            ' inherit the base class
    METHOD BumpIt(Inc AS LONG) AS LONG
      Temp& = Counter + Inc
      INCR Counter
      METHOD = Temp&
    END METHOD
  END INTERFACE
  ' more interfaces could be implemented here
END CLASS

Just like other blocks of code in PowerBASIC, a class is enclosed in the CLASS statement and the END CLASS statement.  Every class is given a text name (in this case "MyClass") so it can be referenced easily in the program.

The INSTANCE statement describes INSTANCE variables for this class. Each object you create from this class definition contains its own private set of any INSTANCE variables.  So, if you had a SHIRT class, you might have an instance variable named COLOR, among others.  Then, if you create two objects from the class, the COLOR instance variable in the first object might contain WHITE, while the second might be BLUE.

Next comes the INTERFACE and END INTERFACE statements.  They define the one interface in this class, and they enclose the methods and properties in this interface.  Every interface is given a text name (in this case "MyInterface") so it can be referenced easily in the program.  You could add any number of additional interfaces to this class if it suited the needs of your program.

The first statement in every Interface Block is the INHERIT statement. As you learned earlier, every interface must contain the three methods of IUNKNOWN as its first three methods.  In this case, INHERIT is a shortcut, so you don't have to type the complete definitions of those methods in every interface.  There are more complex (and powerful) ways to use INHERIT as well, but more about that later.

Finally, we have the METHOD and END METHOD statements.  They are just about identical to a FUNCTION block, but they may only appear in an interface.  In this case, the METHOD is named "BumpIt".  It takes one ByRef parameter, and returns a long integer result.

How do you reference this object?

FUNCTION PBMAIN()
  DIM Stuff AS MyInterface
  LET Stuff = CLASS "MyClass"
  x& = Stuff.BumpIt(77)
END FUNCTION

The first line of PBMain (DIM...) defines an object variable for an interface of the type "MyInterface".  The LET statement creates an object of the CLASS "MyClass", and assigns an object reference to the object variable named "Stuff".  The next line tells PowerBASIC that you want to execute the method "BumpIt", and assign the result to the variable "x&".  It's just that simple!

 

See Also

What is an object, anyway?

What is a Base Class?

What does an Interface look like?

Just what is COM?