How to Compare User-Defined Type Variables in Basic PDS (88479)



The information in this article applies to:

  • Microsoft Basic Professional Development System for MS-DOS 7.0
  • Microsoft Basic Professional Development System for MS-DOS 7.1
  • Microsoft QuickBASIC 4.0, when used with:
    • the operating system: MS-DOS
  • Microsoft QuickBASIC 4.0b, when used with:
    • the operating system: MS-DOS
  • Microsoft QuickBASIC 4.5, when used with:
    • the operating system: MS-DOS

This article was previously published under Q88479

SUMMARY

The relational operators (=, <>, and so on) do not support the comparison of user-defined type variables. You can compare user- defined type variables by converting the variables to strings, then comparing the strings. Use PEEK, VARPTR, and CHR$ to convert a user-defined type variable to a string.

MORE INFORMATION

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

The following example program demonstrates how to compare user-defined type variables using relational operators. Before comparison, the variables are converted to strings.
DECLARE FUNCTION type2str$ (t AS ANY)

TYPE myType
    f1 AS STRING * 2
    f2 AS SINGLE
END TYPE

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

'
' type2str converts a user-defined type variable to a string.
'
FUNCTION type2str$ (t AS myType)
    DEF SEG = VARSEG(t)   ' In case the variable is in a dynamic array.
    FOR i% = 0 TO LEN(t) - 1
        s$ = s$ + CHR$(PEEK(VARPTR(t) + i%))
    NEXT
    DEF SEG
    type2str = s$
END FUNCTION
				

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB88479