Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there an IsVariant property or function?

Status
Not open for further replies.

origapizni

Programmer
Aug 2, 2001
29
US
VB has a number of functions (IsDate, IsNumeric, etc.) to check a variable's type. I could not find one that checks to see if a variable is a Variant, so I wrote the one below. But is there a simpler approach?

Function IsVariant(ByRef TestVar As Variant) As Boolean
' This assumes Objects are not considered variants.

' Default value.
'IsVariant = False

If IsObject(TestVar) = False Then
Dim SaveVar As Variant
SaveVar = TestVar

On Error Resume Next
TestVar = Null
If Err.Number = 0 Then
'only variants can be assigned a null value
IsVariant = True

' Restore the original value.
TestVar = SaveVar
Else
' Since we had an error, the original value of the
' input param was not changed.
Err.Clear
End If

End If

End Function

It works fine; however, I am a little concerned with the amount of code that has to be executed. This code may be executed thousands (or even tens of thousands) of times during execution of a program that I am currently writing.

Maybe this code is ultra-fast, and I'm worrying for nothing, but can anyone come up with anything that will run faster?
 
No, sadly that doesn't work. The VarType function never returns vbVariant in VB. It always returns the type that vb thinks is held in the variant.
 
the functions you meantion don't test the variable type but the data in the variable.

IE IsNumeric() just checks if the data passed to it is numeric or not same with IsDate.

the VarType will only return variant if it is in an array of variants
the typeof statement only works on objects.

Maybe if you could explain what you are trying to do we can provide a way to do it.
 
Duh! You're right, semper.

How about this?

Private Function IsVariant(x) As Boolean
On Error Resume Next
Dim vSafe As Variant
vSafe = x
x = Null
IsVariant = (Err.Number = 0)
x = vSafe
End Function

However, the question is still the same (It's really a request on behalf of a co-worker). He describes his problem thus: "I am working on a generic replacement for the VB "Input #" statement. (VB has some problems with its version.) It will automatically assign values from a comma-delimited file to a series of variables of any type (including Variant). I need to know if the variable is a Variant in order to do the proper assignment."

I don't know the details... If he finds out that VB has no built-in way (property?) to identify the variable type, he'll just go with the function above. He's hoping for a built-in since it would likely be faster.

Thanks so far to all respondents.
 
No, there is no built-in function that reliably reurns the Variant stust of a variable (except under the conditions described by SemperFiDownUnda)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top