Wednesday, June 11, 2025

Examples compiled with Harbour + HBUtil

hb32\contrib\hbutil. To compile:

hbmk2 test1.prg hbutil.hbc

1 - Variable declaration and automatic initialization

LOCAL | STATIC | PUBLIC | PRIVATE <var, ...> AS <TYPE>

// Extended commands (override) 

// Declare and initialize variables
LOCAL nPos(1), nSize AS INTEGER
LOCAL nPrice, nTotal(3) AS MONEY // nPrice:=0.00, nTotal:=0.000
LOCAL tDat1, tToday(), tDat3 AS TIMESTAMP
LOCAL dDat1, dToday(), dDat3 AS DATE // dDat1:=CTOD(''), dToday:=DATE()
LOCAL lStart, lEnd AS LOGICAL
LOCAL hItem AS HASH
LOCAL aTypes[3], aItems AS ARRAY
// Harbour native (just declare variables)
LOCAL oBrw AS CLASS TBROWSE
LOCAL oCol, oGet, oPrn AS OBJECT
LOCAL bInit, bAction AS BLOCK

CLS

? '    LOGICAL', lStart, lEnd    // .F. .F. 
? 'INTEGER (N)', nPos, nSize     // 0          0
? '  MONEY (N)', nPrice, nTotal  //          0.00          0.000
? '  TIMESTAMP', tDat1           //   /  /   00:00:00.000
? '           ', tToday          // 06/18/25 14:38:09.398
? '           ', tDat3           //   /  /   00:00:00.000
? '       DATE', dDat1, dDat3    //   /  /     /  /  
? '           ', dToday          // 06/18/25
? oBrw, oCol, bInit              // NIL NIL NIL

// HBUtil: Extended commands 
PRIVATE dVar1, dVar2() AS DATE
? 'PRIVATE', dVar1, dVar2        //   /  /   06/18/25

2 - VALIDATE PARAMETERS: FUNCTION | METHOD

Define rules for automatic validation (RTE):

  1. Default value (argument omitted) and validate type
  2. Validate type (required argument)
  3. Validate type and allowed to omit argument
  4. Validate if argument passed by reference
  5. Validate type and non-empty value (No spoilers for now 😃)

PROCEDURE MAIN()
   test2( , 5 )  // OK
   test2B()      // OK
   test2C()      // runtime error
   RETURN

FUNCTION test2( cP1, nLeft = 0, nRight = MAXCOL() )
   ? cP1    // NIL
   ? nLeft  // 5  throw RTE if type not equal to N
   ? nRight // 79 throw RTE if type not equal to N
   RETURN NIL
   
FUNCTION test2B( cP1, cMode = 'A', dToday AS DATE OPTIONAL )
   ? cP1    // NIL
   ? cMode  // "A" throw RTE if type not equal to C
   ? dToday // NIL throw RTE if type not equal to D, NIL is allowed
   RETURN NIL
   
FUNCTION test2C( cP1, lNew = .F., dSale AS DATE )
   // lNew  - throw RTE if type not equal to L
   // dSale - throw RTE if type not equal to D
   RETURN NIL

3 - REQUIRED <var, ...>

METHOD uploadFile( cFileName ) CLASS TWhatsApp
   
   REQUIRED cFileName, ::cToken, ::cURL RETURN .F.
   // If any variable or property is empty, execute RETURN .F.
 
   // In this context, will try to send a "signal" to your class with the variable name
   ...
   ...
   RETURN ::post()  
4 - IN: search value in another value (string, array or hash)
PROCEDURE Main()
   LOCAL cString := "Harbour"
   LOCAL hHash   := { "A" => Date(), 1 => "B" }
   LOCAL aSub    := { 1, 2 }
   LOCAL aArray  := { 1, Date(), aSub }
   
   ? ("A"   IN cString)             // result: .F.
   ? ("arb" IN cString)             // result: .T.
   ? ("A" IN hHash)                 // result: .T.
   ? ("B" IN hHash)                 // result: .F.
   ? (1   IN hHash)                 // result: .T.
   ? ("A"    IN aArray)             // result: .F.
   ? (Date() IN aArray)             // result: .T.
   ? (aSub   IN aArray)             // result: .T.
   RETURN
   // this is the same example as xHarbour (but they forgot the parentheses :-)
Note:
  • HbUtil will be included exclusively in a special consultancy.
  • In the official Harbour group, I offered the complete source code so that anyone can use it, but no one responded.
See:

No comments:

DBU for Windows 32/64 bits (+small features) and other alternatives

Em Windows vocĂȘ tem mĂșltiplas opçÔes para gerenciar arquivos DBF: