TExStruct

Source: source/classes/texstruc.prg

Standalone class

TExStruct provides C-like structure definitions within Harbour code. It allows defining typed data members with optional default values and sizes, and supports compiler-style STRUC...END STRUC and MEMBER...AS...INIT...SIZE commands. An integrated error handler enables direct member access through the dot notation (oStruc:MemberName).

Key DATA Members

DATATypeDescription
aMembersArrayArray of member definitions, each containing name, type, default value, and size
lGenErrorLogicalGenerate runtime error on undefined member access (vs. returning nil)

Methods

MethodDescription
New()Create an empty extended structure
AddMember( xName, cType, xDefault, nSize )Add a member with name, type ("C", "N", "D", "L", "A"), optional default, and size
MemberList()Return an array of member names defined in the structure
GetMember( xName )Retrieve the value of a member by name or index

Compiler Commands

STRUC oPerson
   MEMBER Name   AS "C" INIT "John"  SIZE 30
   MEMBER Age    AS "N" INIT 0       SIZE 3
   MEMBER Active AS "L" INIT .T.
END STRUC

// Direct member access:
oPerson:Name    // Returns "John"
oPerson:Age := 35

Example: Define Person Structure

#include "FiveWin.ch"

function Main()

   local oPerson := TExStruct():New()

   oPerson:AddMember( "Name",   "C", "John Doe", 30 )
   oPerson:AddMember( "Age",    "N", 0, 3 )
   oPerson:AddMember( "Active", "L", .T. )
   oPerson:AddMember( "Salary", "N", 0, 10.2 )

   ? oPerson:Name           // Outputs: John Doe
   ? oPerson:Age            // Outputs: 0
   ? oPerson:MemberList()   // Outputs: { "Name", "Age", "Active", "Salary" }

return nil

Notes

See Also