WITH OBJECT

WITH OBJECT

Identifies an object to receive multiple messages.

Syntax

       WITH OBJECT <object>
          :<message1>
          [<statements>]
          [:<messageN>]
       END

Arguments

WITH OBJECT <object> <object> is the symbolic name of a variable that holds a reference to an object. The object is to receive messages in the WITH OBJECT control structure.

:<message> All expressions that begin with the send operator in the OBJECT WITH block are sent as messages to <object>.

Description

The WITH OBJECT control structure delimits a block of statements where the object variable <object> receives multiple messages in abbreviated form. The name of the object variable can be omitted from a message sending expression, so that only the send operator (:) followed by the message to send must be typed.

WITH OBJECT basically relieves a programmer from typing. The name of the object variable is typed only once at the beginning of the WITH OBJECT block, and all subsequent messages sent to the object start with the send operator, omitting the object’s variable name.

Example

       // The example builds a simple database browser using a TBrowse object.
       // The columns added to the object and the cursor navigation logic is
       // programmed using the WITH OBJECT control structure.
       #include "inkey.ch"
       PROCEDURE Main
          LOCAL oTBrowse, aFields, cField, nKey
          USE Customer
          aFields := Array( FCount() )
          AEval( aFields, {|x,i| aFields[i] := FieldName(i) } )
          oTBrowse := TBrowseDB()
          WITH OBJECT oTBrowse
             FOR EACH cField IN aFields
                :addColumn( TBColumnNew( cField, FieldBlock( cField ) ) )
             NEXT
          END
          nKey := 0
          DO WHILE nKey <> K_ESC
             WITH OBJECT oTBrowse
                DO WHILE .NOT. :stabilize()
                ENDDO
                nKey := Inkey(0)
                SWITCH nKey
                   CASE K_UP
                     :up()       ; EXIT
                   CASE K_DOWN
                     :down()     ; EXIT
                   CASE K_LEFT
                     :left()     ; EXIT
                   CASE K_RIGHT
                     :right()    ; EXIT
                   CASE K_PGUP
                     :pageUp()   ; EXIT
                   CASE K_PGDN
                     :pageDown() ; EXIT
                   CASE K_HOME
                     :home()     ; EXIT
                   CASE K_END
                     :end()      ; EXIT
                END
             END
          ENDDO
          CLOSE Customer
       RETURN

Seealso

CLASS, METHOD

DATA

 

DATA

 

Alternate syntax for VAR: instance variable for the objects.

Syntax

      DATA <DataName1> [,<DataNameN>] [ AS <type> ] [ INIT <uValue> ]
      [[EXPORTED | VISIBLE] | [PROTECTED] | [HIDDEN]] [READONLY | RO]

Arguments

<DataName1> Name of the DATA

<type> Optional data type specification from the following: Character, Numeric, Date, Logical, Codeblock, Nil.

<uValue> Optional initial value when creating a new object.

EXPORTED Specifies that this DATA is accessible to functions and methods outside of the class. VISIBLE is a synonym for EXPORTED.

PROTECTED Specifies that this DATA is only accessible to functions and methods within this class and its subclasses.

HIDDEN Specifies that this DATA is only accessible to the class where it was defined, and is not inherited by the subclasses.

READONLY Restricts assignment to the variable. If specified with the EXPORTED clause, assignment is only permitted from the current class and its subclasses. If specified with the PROTECTED clause, assignment is only permitted from the current class. RO is a synonym for READONLY.

Description

DATA elements can also be thought of as the “properties” of an object. They can be of any data type, including codeblock. Once an object has been created, the DATA elements are referenced with the colon (:) as in MyObject:Heading := “Last name”. Usually a class also defines methods to manipulate the DATA.

You can use the “AS <type>” clause to enforce that the DATA is maintained as a certain type. Otherwise it will take on the type of whatever value is first assigned to it.

Use the “INIT <uValue>” clause to initialize that DATA to <uValue> whenever a new object is created.

VAR can be a synonym for DATA, or it can use a slightly different syntax for compatibility with other dialects.

Examples

      CREATE CLASS TBColumn

         DATA Block      // Code block to retrieve data for the column
         DATA Cargo      // User-definable variable
         DATA ColorBlock // Code block that determines color of data items
         DATA ColSep     // Column separator character
         DATA DefColor   // Array of numeric indexes into the color table
         DATA Footing    // Column footing
         DATA FootSep    // Footing separator character
         DATA Heading    // Column heading
         DATA HeadSep    // Heading separator character
         DATA Width      // Column display width
         DATA ColPos     // Temporary column position on screen

         METHOD New()    // Constructor

      ENDCLASS

Compliance

Harbour

Platforms

All

Seealso

Object Oriented Programming, CLASS, METHOD, CLASSDATA, VAR

CLASSDATA

 

CLASSDATA

Define a CLASSDATA variable for a class (NOT for an Object!)

Syntax

      CLASSDATA <DataName1> [,<DataNameN>] [ AS <type> ] [ INIT <uValue> ]

Arguments

<DataName1> Name of the DATA

<type> Optional data type specification from the following: Character, Numeric, Date, Logical, Codeblock, Nil

<uValue> Optional initial value at program startup

Description

CLASSDATA variables can also be thought of as the “properties” of an entire class. Each CLASSDATA exists only once, no matter how many objects are created. A common usage is for a counter that is incremented whenever an object is created and decremented when one is destroyed, thus monitoring the number of objects in existence for this class.

You can use the “AS <type>” clause to enforce that the CLASSDATA is maintained as a certain type. Otherwise it will take on the type of whatever value is first assigned to it. Use the “INIT <uValue>” clause to initialize that DATA to <uValue> whenever the class is first used.

Examples

      CREATE CLASS TWindow
         DATA   hWnd, nOldProc
         CLASSDATA lRegistered AS LOGICAL
      ENDCLASS

Compliance

Harbour

Platforms

All

Seealso

Object Oriented Programming, CLASS, METHOD, DATA

VAR

VAR

Alternate syntax for VAR: instance variable for the objects.

Syntax

      VAR <DataName1> [, <DataNameN>] [ AS <type> ] [ INIT <uValue> ]
      [[EXPORTED | VISIBLE] | [PROTECTED] | [HIDDEN]] [READONLY | RO]

Arguments

<DataName1> Name of the VAR

<type> Optional data type specification from the following: Character, Numeric, Date, Logical, Codeblock, Nil.

<uValue> Optional initial value when creating a new object.

EXPORTED Specifies that this VAR is accessible to functions and methods outside of the class. VISIBLE is a synonym for EXPORTED.

PROTECTED Specifies that this VAR is only accessible to functions and methods within this class and its subclasses.

HIDDEN Specifies that this VAR is only accessible to the class where it was defined, and is not inherited by the subclasses.

READONLY Restricts assignment to the variable. If specified with the EXPORTED clause, assignment is only permitted from the current class and its subclasses. If specified with the PROTECTED clause, assignment is only permitted from the current class. RO is a synonym for READONLY.

Description

VAR elements can also be thought of as the “properties” of an object. They can be of any data type, including codeblock. Once an object has been created, the VAR elements are referenced with the colon (:) as in MyObject:Heading := “Last name”. Usually a class also defines methods to manipulate the VAR.

You can use the “AS <type>” clause to enforce that the VAR is maintained as a certain type. Otherwise it will take on the type of whatever value is first assigned to it.

Use the “INIT <uValue>” clause to initialize that VAR to <uValue> whenever a new object is created.

VAR can be a synonym for VAR, or it can use a slightly different syntax for compatibility with other dialects.

Examples

      CREATE CLASS TBColumn

         VAR Block      // Code block to retrieve data for the column
         VAR Cargo      // User-definable variable
         VAR ColorBlock // Code block that determines color of data items
         VAR ColSep     // Column separator character
         VAR DefColor   // Array of numeric indexes into the color table
         VAR Footing    // Column footing
         VAR FootSep    // Footing separator character
         VAR Heading    // Column heading
         VAR HeadSep    // Heading separator character
         VAR Width      // Column display width
         VAR ColPos     // Temporary column position on screen

         METHOD New()   // Constructor

      ENDCLASS

Compliance

Harbour

Platforms

All

Seealso

Object Oriented Programming, CLASS, METHOD, CLASS VAR, VAR

ON ERROR

ON ERROR

Designate a method as an error handler for the class

Syntax

      ON ERROR <MethodName>( [<params,...>] )

Arguments

<MethodName> Name of the method to define

<params, …> Optional parameter list

Description

ON ERROR is a synonym for ERROR HANDLER. It names the method that should handle errors for the class being defined.

Examples

      CREATE CLASS TWindow
         ON ERROR  MyErrHandler()
      ENDCLASS

Compliance

Harbour

Platforms

All

Seealso

Object Oriented Programming, ERROR HANDLER, CLASS, METHOD, DATA

MESSAGE

MESSAGE

Route a method call to another Method

Syntax

      MESSAGE <MessageName>   METHOD <MethodName>( [<params,...>] )

      MESSAGE <MessageName>() METHOD <MethodName>( [<params,...>] )

Arguments

<MessageName> The pseudo-method name to define

<MethodName> The method to create and call when <MessageName> is invoked.

<params, …> Optional parameter list for the method

Description

The MESSAGE command is a seldom-used feature that lets you re-route a call to a method with a different name. This can be necessary if a method name conflicts with a public function that needs to be called from within the class methods.

For example, your app may have a public function called BeginPaint() that is used in painting windows. It would also be natural to have a Window class method called :BeginPaint() that the application can call. But within the class method you would not be able to call the public function because internally methods are based on static functions (which hide public functions of the same name).

The MESSAGE command lets you create the true method with a different name (::xBeginPaint()), yet still allow the ::BeginPaint() syntax to call ::xBeginPaint(). This is then free to call the public function BeginPaint().

Examples

      CREATE CLASS TWindow
         VAR    hWnd, nOldProc
         METHOD New( ) CONSTRUCTOR
         MESSAGE BeginPaint METHOD xBeginPaint()
      ENDCLASS

Compliance

Harbour

Platforms

All

Seealso

METHOD, DATA, CLASS, Object Oriented Programming

ERROR HANDLER

ERROR HANDLER

Designate a method as an error handler for the class

Syntax

      ERROR HANDLER <MethodName>( [<params,...>] )

Arguments

<MethodName> Name of the method to define

<params, …> Optional parameter list

Description

ERROR HANDLER names the method that should handle errors for the class being defined.

Examples

      CREATE CLASS TWindow
         ERROR HANDLER  MyErrHandler()
      ENDCLASS

Compliance

Harbour

Platforms

All

Seealso

Object Oriented Programming, ON ERROR, CLASS, METHOD, DATA

ENDCLASS

ENDCLASS

End the declaration of a class.

Syntax

      ENDCLASS

Arguments

(This statement has no arguments)

Description

ENDCLASS marks the end of a class declaration. It is usually followed by the class methods that are not INLINE.

Examples

      CREATE CLASS TWindow
         VAR hWnd, nOldProc
      ENDCLASS

Compliance

Harbour

Platforms

All

Seealso

Object Oriented Programming, CLASS, METHOD, DATA

CLASS VAR

CLASS VAR

Define a CLASS VAR variable for a class (NOT for an Object!)

Syntax

      CLASS VAR <DataName1> [, <DataNameN>] [ AS <type> ] [ INIT <uValue> ]

Arguments

<DataName1> Name of the VAR

<type> Optional data type specification from the following:

Character, Numeric, Date, Logical, Codeblock, Nil

<uValue> Optional initial value at program startup

Description

CLASS VAR variables can also be thought of as the “properties” of an entire class. Each CLASS VAR exists only once, no matter how many objects are created. A common usage is for a counter that is incremented whenever an object is created and decremented when one is destroyed, thus monitoring the number of objects in existence for this class.

You can use the “AS <type>” clause to enforce that the CLASS VAR is maintained as a certain type. Otherwise it will take on the type of whatever value is first assigned to it.

Use the “INIT <uValue>” clause to initialize that VAR to <uValue> whenever the class is first used.

Examples

      CREATE CLASS TWindow
         VAR   hWnd,  nOldProc
         CLASS VAR lRegistered AS LOGICAL
      ENDCLASS

Compliance

Harbour

Platforms

All

Seealso

Object Oriented Programming, CLASS, METHOD, VAR

CLASS

 

CLASS

 

Define a Class for Object Oriented Programming

Syntax

      [CREATE] CLASS <ClassName> [ <FROM, INHERIT> <SuperClass1> [,<SuperClassN>] ]  [STATIC]

Arguments

<ClassName> Name of the class to define. By tradition, Harbour classes start with “T” to avoid collisions with user- created classes.

<SuperClass1…n> The Parent class(es) to use for inheritance. Harbour supports Multiple Inheritance.

STATIC This clause causes the class function to be declared as a static function. It will therefore not be available outside the current module.

Description

CLASS creates a class from which you can create objects. The CLASS command begins the class specification, in which the DATA elements (also known as instance variables) and METHODS of the class are named. The following scoping commands may also appear. They control the default scope of DATA and METHOD commands that follow them.

         EXPORTED:
         VISIBLE:
         HIDDEN:
         PROTECTED:

    The class specification ends with the END CLASS command.

Classes can inherit from multiple <SuperClasses>, and the chain of inheritance can extend to many levels.

A program uses a Class by calling the Class Constructor, usually the New() method, to create an object. That object is usually assigned to a variable, which is used to access the DATA elements and methods.

Harbour’s OOP syntax and implementation supports Scoping (Protect, Hidden and Readonly) and Delegating, and is largely compatible with Class(y)(tm), TopClass(tm) and Visual Objects(tm).

Examples

      CREATE CLASS TBColumn

         VAR Block      // Code block to retrieve data for the column
         VAR Cargo      // User-definable variable
         VAR ColorBlock // Code block that determines color of data items
         VAR ColSep     // Column separator character
         VAR DefColor   // Array of numeric indexes into the color table
         VAR Footing    // Column footing
         VAR FootSep    // Footing separator character
         VAR Heading    // Column heading
         VAR HeadSep    // Heading separator character
         VAR Width      // Column display width
         VAR ColPos     // Temporary column position on screen

         METHOD New()    // Constructor

      ENDCLASS

Compliance

Harbour

Platforms

All

Seealso

HBClass(), Object Oriented Programming, DATA, METHOD