Custom Variant Wrapper for TDataset - Part 6

In Part 5 we discussed casting our custom variant to and from other data types.  In this part we will discuss implementing dynamic functions and procedures.   Dynamic functions are implemented by overriding TPublishableVariantType.DoFunction and dynamic procedures are implemented by overriding TPublishableVariantType.DoProcedure.

TPublishableVariantType.DoFunction accepts four arguments; Dest which is a variant that will receive the return value (Result) of the function, V which is an instance of our custom variant, Name which is the name of the function and Arguments which is a variant array holding any arguments that may have been passed to our function.  If Name matches the name of one of the dynamic functions that you have implemented DoFunction must return True to indicate that the function has been processed and place the return value of the function in Dest.  If Name does not match the name of one of your dynamic functions you must return False.

To illustrate how all this works, I have implemented two dynamic functions for the TDataset wrapper; FieldCount and Fields.  These are exactly the same as the corresponding TDataset functions.  Our DoFunction implementation looks like this:


function TDatasetVariant.DoFunction(var Dest: TVarData;
const V: TVarData;
const Name: string;
const Arguments: TVarDataArray): Boolean;
begin
if (CompareText(Name, 'FieldCount') = 0) then
begin
// FieldCount - Returns the number of fields in the dataset
Result := True;
Variant(Dest) := TDatasetData(V).VDataset.FieldCount;
end else if ((CompareText(Name, 'Fields') = 0) and
(Length(Arguments) >= 1)) then
begin
// Fields(index: Integer) - Returns a reference to the field whose index
// is given by Arguments[0].
Result := True;
Variant(Dest) :=
VarFieldCreate(TDatasetData(V).VDataset.Fields[Variant(Arguments[0])]);
end else
Result := False;
end;

DoProcedure is implemented in exactly the same way as DoFunction except there is no Dest argument to worry about.

Leave a Reply