Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Quick comparison among UDTs

Total Hit ( 2750)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


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:

Click here to copy the following block
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:

Click here to copy the following block
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:

Click here to copy the following block
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:

Click here to copy the following block
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.


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.