## UDT Comparison, confused [solved]

General FreeBASIC programming questions.
Posts: 2149
Joined: May 24, 2007 22:10
Location: The Netherlands

### UDT Comparison, confused [solved]

Why is "pos1 = pos2" allowed, but "if (pos1 = pos2) then" not?

Code: Select all

`type position  x as integer  y as integerend typedim as position pos1, pos2pos1 = pos2if (pos1 = pos2) then  beepend if`
D.J.Peters
Posts: 8180
Joined: May 28, 2005 3:28
Contact:

### Re: confused

badidea wrote:Why is "pos1 = pos2" allowed, but "if (pos1 = pos2) then" not?

pos1 = pos2 is an asignment
(owerwrite pos1.x with pos2.x and pos1.y with pos2.y)

if (pos1 = pos2) then
is a logical expression
(compare all members from pos1 with all members of pos2)

you can implement it self

here are one solution

Code: Select all

`type position  declare constructor (x_ as integer=0,y_ as integer=0)  as integer x,yend typeconstructor position (x_ as integer=0,y_ as integer=0) this.x=x_ this.y=y_end constructoroperator =(byref a as position,byref b as position) as integer  return ((a.x = b.x) and (a.y = b.y))end operatordim as position pos1=position(1,2)dim as position pos2=position(2,2)dim as position pos3=position(1,2)if (pos1=pos2) then  ? "pos1 same as pos2"elseif (pos1=pos3) then  ? "pos1 same as pos3"end ifsleep`
Last edited by D.J.Peters on Nov 26, 2010 6:19, edited 1 time in total.
Richard
Posts: 3030
Joined: Jan 15, 2007 20:44
Location: Australia
D.J.Peters is right. You have not defined an equality test for your UDT. This is the quickest fix.

Code: Select all

`type position  x as integer  y as integerend typedim as position pos1, pos2pos1 = pos2if (pos1.x = pos2.x) And (pos1.y = pos2.y) then  beepend if `
Posts: 2149
Joined: May 24, 2007 22:10
Location: The Netherlands
Thanks, I know the 'quick fix', but I like the operator statement. Haven't used that before.

@edit:
Is there a 'memory compare' function that just compares all the bits in the 2 UDTs?
j_milton
Posts: 458
Joined: Feb 11, 2010 17:35
Is there a 'memory compare' function that just compares all the bits in the 2 UDTs?

Here is one:

Code: Select all

`Function memcompare(ByVal a As Any Ptr, ByVal b As Any Ptr, ByVal n As Integer) As Integer   'returns "true" if memory pointed to by a and b contains same data, else false   'n is number of bytes to test   Do While n > 0      If *CPtr( Byte Ptr,a) <> *CPtr( Byte Ptr,b) Then Return 0      n -= 1      a += 1      b += 1   Loop   Function = -1End FunctionDim As Integer a, ba = 1b = 1If memcompare(@a, @b, SizeOf(a)) Then    Print "same"Else   Print "different"EndIfa = 2If memcompare(@a, @b, SizeOf(a)) Then    Print "same"Else   Print "different"EndIfb = 2If memcompare(@a, @b, SizeOf(a)) Then    Print "same"Else   Print "different"EndIfsleep`
Richard
Posts: 3030
Joined: Jan 15, 2007 20:44
Location: Australia
badidea wrote:Is there a 'memory compare' function that just compares all the bits in the 2 UDTs?

A memory block compare of UDTs may not work with UDTs that contain any strings, pointers or arrays. Also uninitialised alignment bytes will cause problems in some types.
You really need to do the compare intelligently knowing the structure.