VariantChangeType
f90VB Modules
f90VBDefs, f90VBVariants
Summary
Converts a variant from one type to another. The source and destination can be the same variant, in which case the subroutine performs a conversion in-place.
Syntax
| subroutine VariantChangeType | (VarSrc, VarDest, vtType, iRet, lcid, flags) |
| type(VARIANT),intent(in):: | VarSrc |
| type(VARIANT),intent(inout):: | VarDest |
| integer(VARTYPE_KIND),intent(in):: | vtType |
| integer(HRESULT_KIND),intent(out),optional:: | iRet |
| integer(LCID_KIND),intent(in),optional:: | lcid |
| integer(WORD_KIND),intent(in),optional:: | flags |
Arguments
VarSrc [Input]
Variant whose type is to be changed (source variant).
VarDest [Input/Output]
Variant that will contain the coerced contents of VarSrc. VarSrc and VarDest can be the same variant, in which case the conversion is performed in-place. VarDest must be an initialized variant.
vtType [Input]
The VT-Type to which VarSrc is to be converted.
lcid [Input/Optional]
A locale identifier.
flags [Input/Optional]
Flags that control the coercion of VarSrc. The only defined flag is VARIANT_NOVALUEPROP, which prevents the function from attempting to coerce an object to a fundamental type by getting the object’s Value property. Applications should set this flag only if necessary, because it makes their behavior inconsistent with other applications
iRet [Output/Optional]
Upon return, iRet contains S_OK or an error code. See comments for more information.
Comments
This subroutine can be used to coerce a variant of one type into another. Variants that have the VT_BYREF flag are de-referenced during the conversion, so the destination variant will not contain the VT_BYREF flag. A variant containing an object is coerced to a value by invoking the object's Value property (DISPID_VALUE).
If VarSrc and VarDest are the same variable, and VarSrc contains a BString or a reference to an object, VariantChangeType will call StrFree or the object’s Release method during the conversion process, to release the original contents of the variant.
Note that you cannot use this subroutine to change the type of a variant containing a Safe Array.
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 | The source variant type or the value of vtType is not a valid type of variant. | |
| DISP_E_OVERFLOW | The data in VarSrc does not fit in the destination type. | |
| DISP_E_TYPEMISMATCH | VarSrc could not be coerced to the specified type. | |
| E_INVALIDARG | One of the arguments is invalid. | |
| E_OUTOFMEMORY | Memory could not be allocated for the conversion. | |
| program VariantChangeTypeExample |
!Demonstrates the use of subroutine VariantChangeType
!Copyright 1999-2000, Canaima Software
!All rights reserved
!NOTE: subroutine vtTypeToStr is defined in the example
! for subroutine VariantCopy
use f90VBDefs
use f90VBBStrings
use f90VBVariants
implicit none
type(VARIANT)::vVar1, vVar2, vVar3
integer(HRESULT_KIND)::iRet
character(len=20)::TmpStr
!Initialize variant
vVar1 = VariantCreate(VT_BSTR,'10.5')
vVar2 = VariantCreate(iRet)
vVar3 = vVar2
!Print information about vVar1
print *,'Variant vVar1'
call vtTypeToStr(vVar1%varVal%vt,TmpStr)
print *,'vtType:',trim(TmpStr)
call StrCopy(vVar1%varVal%bstrVal,TmpStr)
print *,'BString in vVar1: ', trim(TmpStr)
!Change the type of the vVar1 to a real, store
!the result in vVar2
call VariantChangeType(vVar1,vVar2,VT_R4,iRet)
!Print information about vVar2
print *,''
print *,'Variant vVar2'
call vtTypeToStr(vVar2%varVal%vt,TmpStr)
print *,'vtType:',trim(TmpStr)
print *,'fltVal:',vVar2%varVal%fltVal
!change the type of vVar2 to a double real
!performing the operation in-place
call VariantChangeType(vVar2,vVar2,VT_R8,iRet)
!Print information about vVar2
print *,''
print *,'Variant vVar2'
call vtTypeToStr(vVar2%varVal%vt,TmpStr)
print *,'vtType:',trim(TmpStr)
print *,'dblVal:',vVar2%varVal%dblVal
!change the value of vVar1 to decimal
!performing the operation in-place
!the original BString in vVar1 is released
call VariantChangeType(vVar1,vVar1,VT_DECIMAL,iRet)
!Print information about vVar1
print *,''
print *,'Variant vVar1'
call vtTypeToStr(vVar1%varVal%vt,TmpStr)
print *,'vtType:',trim(TmpStr)
!convert the decimal into a double precision
!for printing purposes
print *,'Decimal in vVar1:', VariantToDouble(vVar1,iRet)
call VariantClear(vVar1)
call VariantClear(vVar2)
call VariantClear(vVar3)
stop
end
Related Topics
| For information about: | See: | |
| Creating a variant | VariantCreate | |
| Copying a variant | VariantCopyInd, VariantCopy | |