origapizni
Programmer
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?
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?