Microsoft KB Archive/88551

= How to Compare User-Defined Type Variables in Visual Basic =

Article ID: 88551

Article Last Modified on 12/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition

-



This article was previously published under Q88551



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 &quot;Type mismatch&quot; 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
 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 &quot;kernel&quot; (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 = &quot;ab&quot; x.f2 = 2 y = x

If type2str(x) = type2str(y) Then Print &quot;x = y&quot; Else Print &quot;x <> y&quot; End If

y.f1 = &quot;ba&quot; If type2str(x) > type2str(y) Then Print &quot;x > y&quot; Else Print &quot;x <= y&quot; End If  End Sub  Press the F5 key to run the program.

The program prints &quot;x = y&quot; and &quot;x <= y&quot; on Form1.

Additional query words: 2.00 3.00 3.10

Keywords: KB88551

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.