Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB5,VB6
Expertise: Advanced
Aug 1, 2000

Quick comparison among UDTs

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 Single
End 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 Boolean
If 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 If
End If

If 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 data
Private Type MyUDT
    item1 As Boolean
    item2(10) As Integer
    item3 As Long
    item4 As Single
    item5 As Double
    item6 As Currency
    item7 As String * 20
End Type
Dim udt1 As MyUDT, udt2 As MyUDT

' init the first UDT
udt1.item1 = 10
udt1.item2(1) = 4
udt1.item3 = 12345
udt1.item4 = 345.567
udt1.item5 = 111.333444
udt1.item6 = 223344.5566
udt1.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 compared
Dim bytes As Long
bytes = LenB(udt1)

' the strings used for the comparison
Dim s1 As String, s2 As String
' make them long enough to host the UDTs
s1 = Space$((bytes + 1) \ 2)
s2 = s1

' copy the UDTs into the strings
CopyMemory ByVal StrPtr(s1), ByVal VarPtr(udt1), bytes
CopyMemory ByVal StrPtr(s2), ByVal VarPtr(udt2), bytes

' now you can perform the comparison
If s1 = s2 Then
    MsgBox "Equal"
Else
    MsgBox "Different"
End If

End 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.
Francesco Balena
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date