When you need to compare two User Defined Type (UDT) variables to check whether they are equal, the only approach that you can follow in plain VB is to compare each individual element of the UDT. For example, say that you have the following Type declaration:
Private Type MyUDT item1 As Boolean item2 As Long item3 As Double item4 As SingleEnd Type
and two MyUDT variables, udt1 and udt2. This code checks whether these variables contain the same values:
If udt1.item1 = udt2.item1 And udt1.item2 = udt2.item2 And udt1.item3 = _ udt2.item3 And udt1.item4 = udt2.item4 Then MsgBox "Equal"Else MsgBox "Different"End If
You can make your code faster if you manually adopt a short-circuit evaluation technique, so that unnecessary comparisons are never performed:
Dim equal As BooleanIf udt1.item1 = udt2.item1 Then If udt1.item2 = udt2.item2 Then If udt1.item3 = udt2.item3 Then If udt1.item4 = udt2.item4 Then equal = True End If End IfEnd IfIf equal MsgBox "Equal"Else MsgBox "Different"End If
However, you trade code linearity with performance, and this approach can really be used for UDTs with more than just a few items.
A better approach is to move the contents of both UDTs into two strings, and then compare the strings. You need the CopyMemory API function to do so, and you must evaluate the exact number of bytes to be moved:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As _ Any, source As Any, ByVal bytes As Long)' a sample UDT structure, that contains almost every possible type of dataPrivate Type MyUDT item1 As Boolean item2(10) As Integer item3 As Long item4 As Single item5 As Double item6 As Currency item7 As String * 20End TypeDim udt1 As MyUDT, udt2 As MyUDT' init the first UDTudt1.item1 = 10udt1.item2(1) = 4udt1.item3 = 12345udt1.item4 = 345.567udt1.item5 = 111.333444udt1.item6 = 223344.5566udt1.item7 = "this is a test"' init the second UDT' (in this test both UDTs contains the same value)udt2 = udt1' the number of bytes to be comparedDim bytes As Longbytes = LenB(udt1)' the strings used for the comparisonDim s1 As String, s2 As String' make them long enough to host the UDTss1 = Space$((bytes + 1) 2)s2 = s1' copy the UDTs into the stringsCopyMemory ByVal StrPtr(s1), ByVal VarPtr(udt1), bytesCopyMemory ByVal StrPtr(s2), ByVal VarPtr(udt2), bytes' now you can perform the comparisonIf s1 = s2 Then MsgBox "Equal"Else MsgBox "Different"End IfEnd Sub
There are a few points you must keep in mind in order to use this technique correctly:
- the UDT can contain only numeric items and fixed-length strings: it can’t contain variable-length strings or object references
- The UDT can contain a static array of any type of data, except variable-length strings or object, but it can’t contain dynamically resized arrays: in other words, the number of elements must be established in the UDT declaration
- You can only compare UDTs for equality: you can’t use this technique to decide whether a UDT is “greater” or “lesser” than another, whatever this might mean in your application
- Fixed-length strings inside the UDT are compared in case-insensitive mode; you can’t use this technique to compare strings without making any distinction between character case.