Show: Delphi C++
Display Preferences

Implementing Unary Operations

From RAD Studio
Jump to: navigation, search

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.

See Also

Personal tools
RAD Studio 10.2 Tokyo
In other languages
Previous Versions
Assistance