How to Compare User-Defined Type Variables in Visual Basic

Last reviewed: June 21, 1995
Article ID: Q88551
The information in this article applies to:
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0 and 3.0
  • Microsoft Visual Basic programming system for Windows, version 1.0

SUMMARY

The relational operators (=, <>, and so on) do not support the comparison of user-defined type variables. However, you can compare user-defined type variables by converting the variables to strings, and then comparing the strings. The Windows version 3.1 API hmemcpy can be used to convert a user-defined type variable to a string.

The hmemcpy API was introduced in Microsoft Windows version 3.1, so this technique requires Windows version 3.1 or later.

MORE INFORMATION

If you attempt to compare user-defined type variables using the relational operators, the error "Type mismatch" is displayed.

The following steps demonstrate how to compare user-defined type variables by first converting the variables to strings and then comparing the strings by using the relational operators.

Step-by-Step Example

1. Start Visual Basic or from the File menu, choose New Project (ALT, F, N)
   if Visual Basic is already running. Form1 is created by default.

  • Enter the following code into the global module:

    Type myType

          f1 As String * 2
          f2 As Single
    
    End Type

       ' Enter the following Declare statement entirely as one, single line:
       Declare Sub hmemcpy Lib "kernel" (hpvDest As Any, hpvSource As Any,
          ByVal cbCopy As Long)
    
    

  • Enter the following code into the general Declarations section of Form1:

       ' type2str converts a user-defined type variable to a string.
       Function type2str (t As myType) As String
          Dim s As String
          s = Space$(Len(t))
          Call hmemcpy(ByVal s, t, Len(t))
          type2str = s
       End Function
    
    

  • Enter the following code into the Form1 Click event procedure:

       Sub Form_Click ()
          Dim x As myType
          Dim y As myType
    
          x.f1 = "ab"
          x.f2 = 2
          y = x
    
          If type2str(x) = type2str(y) Then
             Print "x = y"
          Else
             Print "x <> y"
          End If
    
          y.f1 = "ba"
          If type2str(x) > type2str(y) Then
             Print "x > y"
          Else
             Print "x <= y"
          End If
       End Sub
    
    

  • Press the F5 key to run the program.

    The program prints "x = y" and "x <= y" on Form1.


  • Additional reference words: 1.00 2.00 3.00 3.10
    KBCategory: kbprg kbcode
    KBSubcategory: APrgOther


    THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

    Last reviewed: June 21, 1995
    © 1998 Microsoft Corporation. All rights reserved. Terms of Use.