Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 774
derekerdmann Avatar asked Jun 30 '10 13:06

derekerdmann


2 Answers

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
like image 141
raven Avatar answered Sep 22 '22 02:09

raven


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.

like image 29
derekerdmann Avatar answered Sep 18 '22 02:09

derekerdmann