Demo: Invoking UDFs from UDTs

From: Neo <neo55592_at_hotmail.com>
Date: 9 Aug 2004 14:51:44 -0700
Message-ID: <4b45d3ad.0408091351.1c29c83f_at_posting.google.com>



www.xdb2.com/Example/Ex006.asp demos how to link a user-defined C function (DisplayInfo_r) to a class (person) and then have instances (john, mary) invoke the inherited function.

void FunctionLinkAndInherit_Demo()
{

// Create person in db.

        int* pPerson = T_create();
        T_Name_relate(&pPerson, _T("person"));
        T_relate(&pPerson, &pReCls_g, &pThing_g);


// Link a C function (DisplayInfo_r listed below) to person in db.
T_Fn_relate(&pPerson, DisplayInfo_r, _T("displayInfo"));
// Create age in db.
int* pAge = T_create(); T_Name_relate(&pAge, _T("age")); T_relate(&pAge, &pReCls_g, &pThing_g);
// Create color in db.
int* pColor = T_create(); T_Name_relate(&pColor, _T("color")); T_relate(&pColor, &pReCls_g, &pThing_g);

// Create john, an instance of person.
// Set his age to 35.

        int* pJohn = T_create();
        T_Name_relate(&pJohn, _T("john"));
        T_relate(&pJohn, &pReCls_g, &pPerson);
        T_Val_relate(&pJohn, &pAge, _T("35"));

// Create mary, an instance of person.
// Set her age to 25, and color to brown.

        int* pMary = T_create();
        T_Name_relate(&pMary, _T("mary"));
        T_relate(&pMary, &pReCls_g, &pPerson);
        T_Val_relate(&pMary, &pAge, _T("25"));
        T_Val_relate(&pMary, &pColor, _T("brown"));


// Get thing for function displayInfo.
int* pFnDispInfo = Str_getDefT(_T("displayInfo"));

// Have john execute function displayInfo
// which he inherits from his class (person).
// Following displays "john cls person. john is 35." in a dialog
box.

        T_Fn_execute_r(pJohn, pFnDispInfo);

// Have mary execute function displayInfo
// which she inherits from her class (person).
// Following displays "mary cls person. mary is 25. mary is
brown."

        T_Fn_execute_r(pMary, pFnDispInfo); }

/********************************************************************
Displays pT's values. (ie 'john is 35. john is brown.') If pT has no values, displays pT's name. (ie 'john')
********************************************************************/
ERR_CODE DisplayInfo_r(int* pT)
{

    TCHAR sT[kStrSz_g+1] = _T("");
    T_Name_get(pT, sT, kStrSz_g);

    BOOL hasProperty_b = FALSE;
    TCHAR sInfo[kStrSz_g+1] = _T("");
    int* pA[] = {NULL, pT, pS_g, NULL};
    int* pB[] = {NULL, (int*)pA, pN_g, NULL};     while (int* pN=X2(pB)){

        if (pReName_g != N_getV(pN)){
            hasProperty_b = TRUE;

            TCHAR sProp[kStrSz_g+1] = _T("");
            T_Name_get(N_getV(pN), sProp, kStrSz_g);

            TCHAR sVal[kStrSz_g+1] = _T("");
            T_Name_get(N_getO(pN), sVal, kStrSz_g);

            TCHAR sSent[kStrSz_g+1] = _T("");
            _stprintf(sSent, _T("%s %s %s.\n"), sT, sProp, sVal);

            _tcscat(sInfo, sSent);
        }

    }

    if (hasProperty_b){

        MessageBox(NULL, sInfo, kAppName_sg, MB_OK);     }
    else{

        MessageBox(NULL, sT, kAppName_sg, MB_OK);     }

    return kErrNone_g;
}

To have john execute the inherited function via the GUI:

1. Select thing/person/john.
2. Ctrl+E (execute).
3. Select thing/function/displayInfo or thing/person/displayInfo.
4. Ctrl+V (paste).
Received on Mon Aug 09 2004 - 23:51:44 CEST

Original text of this message