UDT Comparison, confused [solved]

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

UDT Comparison, confused [solved]

Postby badidea » Nov 25, 2010 0:50

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

Code: Select all

type position
  x as integer
  y as integer
end type

dim as position pos1, pos2

pos1 = pos2

if (pos1 = pos2) then
  beep
end if
D.J.Peters
Posts: 8180
Joined: May 28, 2005 3:28
Contact:

Re: confused

Postby D.J.Peters » Nov 25, 2010 1:54

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,y
end type
constructor position (x_ as integer=0,y_ as integer=0)
 this.x=x_
 this.y=y_
end constructor

operator =(byref a as position,byref b as position) as integer
  return ((a.x = b.x) and (a.y = b.y))
end operator

dim 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 if
sleep
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

Postby Richard » Nov 25, 2010 1:59

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 integer
end type

dim as position pos1, pos2

pos1 = pos2

if (pos1.x = pos2.x) And (pos1.y = pos2.y) then
  beep
end if
badidea
Posts: 2149
Joined: May 24, 2007 22:10
Location: The Netherlands

Postby badidea » Nov 25, 2010 15:10

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

Postby j_milton » Nov 25, 2010 17:36

badidea wrote:@edit:
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 = -1
End Function

Dim As Integer a, b

a = 1
b = 1
If memcompare(@a, @b, SizeOf(a)) Then
   Print "same"
Else
   Print "different"
EndIf

a = 2
If memcompare(@a, @b, SizeOf(a)) Then
   Print "same"
Else
   Print "different"
EndIf

b = 2
If memcompare(@a, @b, SizeOf(a)) Then
   Print "same"
Else
   Print "different"
EndIf

sleep
Richard
Posts: 3030
Joined: Jan 15, 2007 20:44
Location: Australia

Postby Richard » Nov 25, 2010 20:52

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.

Return to “General”

Who is online

Users browsing this forum: No registered users and 4 guests