VariantAdd
(DecimalAdd, CurrencyAdd)
f90VB Modules
f90VBDefs, f90VBVariants
Summary
Returns the sum of two variants.
Syntax
| type(VARIANT) function VariantAdd | (VarLeft, VarRight, iRet) |
| type(VARIANT),intent(in):: | VarLeft |
| type(VARIANT),intent(in):: | VarRight |
| integer(HRESULT_KIND),intent(out),optional:: | iRet |
Arguments
VarLeft and VarRight [Input]
Two variant values. Must be initialized variants.
iRet [Output/Optional]
Upon return, iRet contains S_OK or an error code. See comments for more information.
Comments
The function adds the two provided variants using the following rules:
| If: | Function returns a: | |
| Both variants are of the string type | Concatenation | |
| One variant is numeric and the other is a string, or both variants are of the date, character, or Boolean type. | Addition. | |
| Both variants are numeric | Addition. | |
| Either variant is NULL (VT_NULL) | NULL variant. | |
| Both variants are empty (VT_EMPTY) | Zero (0) of integer subtype | |
Functions DecimalAdd and CurrencyAdd work exactly the same as VariantAdd, but the arguments and the return value are of type DECIMAL and CURRENCY respectively.
Argument iRet
Indicates success or failure of the subroutine. The following codes can be returned in this argument:
| Value returned in argument iRet | Description | |
| S_OK | Success. | |
| DISP_E_BADVARTYPE | One or both of the passed variant types are not a valid types of variant. | |
| DISP_E_TYPEMISMATCH | At least one of the variants could not be coerced to the appropriate type for this operation. | |
| E_INVALIDARG | One of the arguments is invalid. | |
| E_OUTOFMEMORY | Memory could not be allocated for the operation.. | |
Examples
The example below shows the results of several arithmetic operations performed with different pairs of variants. Note that some of the operations performed may not make any sense, like multiplying a date by another date, or computing the modulus of two dates. Also note that the code for subroutine vtTypeToStr is listed in the example for subroutine VariantCopy, and is not included here for brevity.
| program VariantAddExamples |
!Demonstrates variant arithmetic functions
!Copyright 1999-2000, Canaima Software
!All rights reserved
use f90VBDefs
use f90VBBStrings
use f90VBVariants
use VariantOperations
implicit none
type(VARIANT):: vVar(7), vResult
integer(HRESULT_KIND)::iRet
character(len=20)::vtLeftStr, vtRightStr, vtResStr, vtResVal
integer::i,j,vOp
integer(BSTRHNDL_KIND)::BStrHndl
!initialize variants to different types
vVar(1) = VariantCreate(iRet) !Empty variant
vVar(2)= VariantCreate(VT_NULL,0) !Null variant
vVar(3)= VariantCreate(VT_BSTR,'10') !BString variant
vVar(4)= VariantCreate(VT_I4,10) !integer variant
vVar(5)= VariantCreate(VT_R4,5.0) !real variant
vVar(6) = VariantCreate(VT_DATE,'10/01/1998')
vVar(7) = VariantCreate(VT_DATE,'01/01/1950 20:00')
print *,'Enter an operation:'
print *,'(1:Add, 2:Cat, 3:Sub, 4:Mul, 5:Div,'
print *,' 6:Eqv, 7:Imp, 8:Mod, 9:And, 10:Or,'
print *,'11:Xor, 12:Pow)'
read *,vOp
if (vOp.lt.1 .or. vOp.gt.12) vOp = 1
!print selected operation
print *,'varOperation: ',vOp
print *,''
!print the content of the operands
print *,'Operands:'
write(*,'(a5,a15,a20)') 'I','Type', 'Content'
do i=1,7
call vtTypeToStr(vVar(i)%varVal%vt,vtResStr)
if (vVar(i)%varVal%vt .eq. VT_NULL) then
vtResVal = 'NULL'
elseif (vVar(i)%varVal%vt .eq. VT_EMPTY) then
vtResVal = 'EMPTY'
else
vtResVal=''
BStrHndl=VariantToBString(vVar(i))
call StrCopy(BstrHndl,vtResVal)
call StrFree(BStrHndl)
endif
write(*,'(i5,a15,a20)') i,trim(vtResStr), &
trim(vtResVal)
enddo
print *,''
print *,'Operation results:'
write(*,'(a5,a5,a15,a15,a15,a20)') '','','Type','Type', &
'Type', 'Result'
write(*,'(a5,a5,a15,a15,a15,a20)') 'I','J','vVarLeft', &
'vVarRight', 'Result', 'Content'
!Execute selected operation with different combinations
!of variants
do i=1,7
do j=1,7
!Add the two variants
vResult = varOperation(vOp,vVar(i),vVar(j),iRet)
if (iRet.eq.S_OK) then
!print the vtType of the resulting variant
call vtTypeToStr(vVar(i)%varVal%vt,vtLeftStr)
call vtTypeToStr(vVar(j)%varVal%vt,vtRightStr)
call vtTypeToStr(vResult%varVal%vt,vtResStr)
if (vResult%varVal%vt .eq. VT_NULL) then
vtResVal = 'NULL'
elseif (vResult%varVal%vt .eq. VT_EMPTY) then
vtResVal = 'EMPTY'
else
vtResVal=''
BStrHndl=VariantToBString(vResult)
call StrCopy(BstrHndl,vtResVal)
call StrFree(BStrHndl)
endif
write(*,'(i5,i5,a15,a15,a15,a20)') i,j,trim(vtLeftStr), &
trim(vtRightStr),trim(vtResStr),trim(vtResVal)
endif
enddo
enddo
print *,''
print *,'Note that some operations may not make sense, like'
print *,'multiplying dates or computing their modulus...'
!clear all variants
do i=1,7
call VariantClear(vVar(i))
enddo
call VariantClear(vResult)
stop
end
module VariantOperations
contains
function varOperation(vOp,vVarLeft,vVarRight,iRet)
use f90VBDefs
use f90VBVariants
implicit none
type(VARIANT)::varOperation
integer,intent(in)::vOp
type(VARIANT),intent(in):: vVarLeft,vVarRight
integer(HRESULT_KIND),intent(inout)::iRet
select case (vOp)
case(1)
varOperation=VariantAdd(vVarLeft,vVarRight,iRet)
case(2)
varOperation=VariantCat(vVarLeft,vVarRight,iRet)
case(3)
varOperation=VariantSub(vVarLeft,vVarRight,iRet)
case(4)
varOperation=VariantMul(vVarLeft,vVarRight,iRet)
case(5)
varOperation=VariantDiv(vVarLeft,vVarRight,iRet)
case(6)
varOperation=VariantEqv(vVarLeft,vVarRight,iRet)
case(7)
varOperation=VariantImp(vVarLeft,vVarRight,iRet)
case(8)
varOperation=VariantMod(vVarLeft,vVarRight,iRet)
case(9)
varOperation=VariantAnd(vVarLeft,vVarRight,iRet)
case(10)
varOperation=VariantOr(vVarLeft,vVarRight,iRet)
case(11)
varOperation=VariantXor(vVarLeft,vVarRight,iRet)
case(12)
varOperation=VariantPow(vVarLeft,vVarRight,iRet)
end select
end function varOperation
end module
Related Topics
| For information about: | See: | |
| Concatenating variants | VariantCat | |