Archived Forum Post

Index of archived forum posts

Question:

DataFlex example code question (returning/passing objects)

Apr 08 '15 at 16:45

I've been trying out your newly posted DataFlex examples and have gotten some of them working. Most have minor syntax problems which I’ll be happy to share but one I haven’t been able to figure out is the following. In the XML SearchForTag example (Same problem in a couple of others) the line “Get ComSearchForTag of hoXSearchRoot hoXBeginAfter "fruit" to vXFound” is sending a handle, hoXBeginAfter, to ComSearchForTag which throws an error since it’s expecting a variant.

    Handle hoXml
    Variant vXSearchRoot
    Handle hoXSearchRoot
    Variant vXBeginAfter
    Handle hoXBeginAfter
    Variant vXFound
    Handle hoXFound
    Boolean iSuccess
    Variant vXSearch
    Handle hoXSearch
    String sTemp1
    ...

    //  Search for all nodes having the tag "fruit";
    Get ComGetSelf of hoXml to vXBeginAfter
    If (IsComObject(vXBeginAfter)) Begin
        Get Create (RefClass(cComChilkatXml)) to hoXBeginAfter
        Set pvComObject of hoXBeginAfter to vXBeginAfter
    End
    Get ComSearchForTag of hoXSearchRoot hoXBeginAfter "fruit" to vXFound

Any idea what the correct code should be? I tried vXBeginAfter but no joy. I’m not all that great with Com stuff so am struggling with this a bit.


Answer

Thanks! Chilkat confesses that it is not an expert in the DataFlex programming language. The DataFlex examples, just as for the examples for all the programming languages on example-code.com, are generated from a single source. For minor syntax errors, please let me know and I'll fix the DataFlex back-end, regenerate, and re-publish.

In the case above, the 1st parameter of the SearchForTag method ("ComSearchForTag") should be a reference to the node in the XML tree where the search should begin. The search will begin at the sub-tree rooted at the caller node, and will traverse breadth-first looking for a match. It will return the 1st match after the "hoXBeginAfter" node is passed. The hoXBeginAfter cannot be NULL/Nothing in the ActiveX implementation (because of technical issues w/ the COM wrappings), and therefore the intent is to pass a node that is also a reference to the caller node (thus the call to GetSelf).

My current understanding of DataFlex/COM (which perhaps is incorrect) is that a method returning a COM object will return a Variant. Thus:

Get ComGetSelf of hoXml to vXBeginAfter
My current understanding (again, which might be incorrect) is that to pass COM object to a method as a parameter, the Handle should be passed. Thus we get the handle (safely) and then pass it:
        If (IsComObject(vXBeginAfter)) Begin
            Get Create (RefClass(cComChilkatXml)) to hoXBeginAfter
            Set pvComObject of hoXBeginAfter to vXBeginAfter
        End
        Get ComSearchForTag of hoXSearchRoot hoXBeginAfter "fruit" to vXFound
If this is incorrect, the code generation can be updated to fix it.

Part of the intent of using a separate COM object for hoXBeginAfter is to show that it can be any node in the XML tree (within the sub-tree rooted at the caller, otherwise the search will always return NULL/Nothing). If the hoXBeginAfter is simply going to be the caller node, the example can be simplified to this (by omitting the call to GetSelf..)

    // Notice we're passing hoXSearchRoot instead of hoXBeginAfter...
    Get ComSearchForTag of hoXSearchRoot hoXSearchRoot "fruit" to vXFound


Answer

I don't think I explained myself as well as I could have. When DataFlex analyzes the Chilkat DLL it creates a class file with all the methods it finds in the DLL. For ComSearchForTag we get this definition:

Function ComSearchForTag Variant llafterPtr String llTag Returns Variant

In the example we have

Get ComSearchForTag of hoXSearchRoot hoXBeginAfter "fruit" to vXFound

In the example, hoXBeginAfter is a handle or integer, in the class the target parameter is a variant. This causes an illegal data conversion or essentially a casting error.


Answer

Thanks! I'll look more closely and will fix the code generation and re-upload.


Answer

I updated the DataFlex examples with your suggestions. Hopefully they're closer to being 100% syntactically correct.