Comparing user types

BlitzMax Forums/BlitzMax Beginners Area/Comparing user types

lonnieh(Posted 2005) [#1]
Hi all! Quick question, is possible to compare user types like so...

Local myType:MyType = MyType.Create(30, 30)
Local myType2:MyType = MyType.Create(35, 35)
if myType = myType2 then
'do some code
end if

? I'm sure its possible. I'd rather do it this way than to compare all of the fields by themselves. I've heard mention of overriding the Compare method, but only in sorting type applications. If overriding the Compare method is the way to go, could someone type me up a quick demonstration?

BTW, BlitzMax owns.

Edit: I'd also like to know if its possible to copy a user type without creating your own Copy method. :-D


dmaz(Posted 2005) [#2]
this works:



didn't test on Macs... should work though.


Sweenie(Posted 2005) [#3]
myType = myType2 won't check if the the types contains the same value, it will just check if they point to the same MyType object.

Local myType:MyType = MyType.Create(30, 30)
Local myType2:MyType = MyType.Create(35, 35)
If myType = myType2
' This code won't execute since myType and MyType2 points to two different objects
Endif

Local myType:MyType = MyType.Create(30, 30)
Local myType2:MyType = myType
If myType = myType2
' This code will execute since myType and myType2 points to the same object
Endif


lonnieh(Posted 2005) [#4]
Ahh, makes perfect sense, thanks for the replies!


Azathoth(Posted 2005) [#5]
Overload the compare method.


Rimmsy(Posted 2005) [#6]
But make sure to return 0 if they are the same because you'll have no end of trouble.
	Method compare(s:Object)
		Local o:system=system(s)
		If o <> Null
			If o.distanceToPlayer=distanceToPlayer
				Return 0
			ElseIf o.distanceToPlayer > distanceToPlayer 
				Return 1 
			Else 
				Return -1
			EndIf
		EndIf
	End Method