The variant data type is a container which can house any other data type (except User Defined Types UDTs).
There is no shortage of people who will scould you for using it, but as with all things (yes even goto's)
there is a place for it and it can be useful and powerful.
The main complaints against it is that it is not defined enough. It does not enforce data types so people can
easily pass the wrong type of thing into a function which causes unexpected blow ups. This is absolutly true.
You will also be told that the type conversions to other types may be slower, or lead to unexpected things
happening. (trying doing a file put or strconv on a variant for example)
However variants are also powerful and can make for quick coding.
If you are just knocking out a quick utility project, there is no need to explicitly define every variable type
or even define the variables at all before you use them. Kick that option explicit to the curb and you will be coding just
as quickly and as flexibly as if you were writing in a script language such as javascript. This is bad for code
quality and readability, but in its proper place is perfectly acceptable. Just keep in mind what you are doing.
You make your own bed, now lay in it so to speak.
The way variants are powerful is in their flexibility. Vb6 does not have function overloading where you can define
different prototypes of a function to work on different data types/returns. With variants however you can gain some
additional functionality. Consider the following:
Function Encode(bArrayStringOrFile, Optional retAsString As Boolean = True) As Variant
Dim b() As Byte
Dim b2() As Byte
Dim cb As New CBase64
On Error Resume Next
If TypeName(bArrayStringOrFile) = "Byte()" Then
b() = bArrayStringOrFile
ElseIf FileExists(bArrayStringOrFile) Then
LoadFile bArrayStringOrFile, b()
Else
If Len(bArrayStringOrFile) = 0 Then Exit Function
b() = StrConv(CStr(bArrayStringOrFile), vbFromUnicode, LANG_US)
End If
b2 = cb.EncodeBytes(b)
If retAsString Then
Encode = StrConv(b2, vbUnicode, LANG_US)
Else
Encode = b2
End If
End Function
This single function can accept a string, byte array, or file path as input.
By default it will return a string as the result, but can also return a byte array.
While this is not as flexible as true function overloading, it is a solid technique and is
much more condensed. This one simple function replaces 6 function overloads. (which could be
scattered about the source module). Where this could get messy is if you needed to pass extra args
or had to contain to much info in your function argument names to self document its functionality.
In these cases it would better to have an alternative similarly named function. Not a deal breaker
by any means.
I would not argue that you should code like this everywhere. These nuances are not immediatly clear
but in carefully selected cases using variants in situations such as these can significantly simplify
your life. (with the added cost of having to understand the implementation and nuances more)
Overall I am glad that vb6 has the variant data type and find it especially useful in knocking
out quick utilities and one time use snippets.
|