Implementing Unary Operations
Go Up to Creating a Class to Enable the Custom Variant Type
To allow the custom variant type to work with standard unary operators ( -, not), you must override the UnaryOp method. UnaryOp
has two parameters: the value of the operand and the operator. Implement this method to perform the operation and return the result using the same variable that contained the operand.
For example, the following UnaryOp
method comes from the TComplexVariantType
defined in the VarCmplx unit:
Delphi:
procedure TComplexVariantType.UnaryOp(var Right: TVarData; const Operator: TVarOp);
begin
if Right.VType = VarType then
case Operator of
opNegate:
TComplexVarData(Right).VComplex.DoNegate;
else
RaiseInvalidOp;
end
else
RaiseInvalidOp;
end;
Note that for the logical not operator, which does not make sense for complex values, this method calls RaiseInvalidOp
to cause a runtime error.