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
| DATA | Type | Description |
|---|---|---|
aMembers | Array | Array of member definitions, each containing name, type, default value, and size |
lGenError | Logical | Generate runtime error on undefined member access (vs. returning nil) |
Methods
| Method | Description |
|---|---|
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
- When
lGenErroris.F.(default), accessing an undefined member returnsnilinstead of raising an error, allowing safe member probing. - Member types follow standard Harbour type indicators: "C" (character), "N" (numeric), "D" (date), "L" (logical), "A" (array).
- The
STRUC...END STRUCcommand provides a C-like syntax for defining structured data in Harbour source code. - The
SIZEparameter is informational; for numeric types it can specify the total width and decimal places (e.g.,10.2). - Direct member access (
oPerson:Name) works through TExStruct's integrated error handler, which intercepts unknown method calls and routes them to member get/set operations.