Quick comparison among UDTs

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 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.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as