derekerdmann
derekerdmann

Reputation: 18262

VB6 Object Comparison

What VB6 method allows two custom objects of the same type (defined in a class module) to be compared to each other? I think there's an equivalent to Java's compareTo method, but I can't find it anywhere.

Upvotes: 6

Views: 3112

Answers (2)

derekerdmann
derekerdmann

Reputation: 18262

For others who may be wondering about the same question:

After doing a lot of looking around, it seems like VB6 doesn't have any kind of built-in compareTo or equals methods, like Java does.

I forgot that in Java, compareTo is defined in the java.lang.Comparable interface. Since interfaces are so broken in VB6, even if you wrote your own Comparable interface, you would have to call your object's Comparable_compareTo method unless it was declared as Comparable, which is pointless.

Bottom line: if you want compareTo or equals methods in your VB6 classes, just put them in.

Upvotes: 1

raven
raven

Reputation: 18145

If by "compare" you mean "are they the same type?", you can you the TypeName function:

If (object1 <> Nothing) and (object2 <> Nothing) then
  If (TypeName(object1) = TypeName(object2)) Then
    Debug.Print "object types are the same"
  Else
    Debug.Print "object types are NOT the same"
  End If
End If

If by "compare" you mean "do they reference the same object in memory?", you can use the Is operator:

If (object1 Is object2) Then
  Debug.Print "objects references are the same"
Else
  Debug.Print "objects references are NOT the same"
End If

Upvotes: 7

Related Questions