IonicWind Software

IWBasic => General Questions => Topic started by: Allan on August 20, 2008, 06:28:26 PM

Title: Comparing DBDATE values
Post by: Allan on August 20, 2008, 06:28:26 PM
I am trying to compare two DBDATE variables and it does not work as hoped.

' console - dbdate.eba
/*
TYPE DBDATE
WORD year
        WORD month
        WORD day
ENDTYPE
*/

OPENCONSOLE

DBDATE aDate, bDate

aDate.day = 21
aDate.month = 8
aDate.year = 2008

bDate.day = 1
bDate.month = 8
bDate.year = 2008

DO
bDate.day = bDate.day + 1
PRINT Str$(bDate.day)+"/"+Str$(bDate.month)+"/"+Str$(bDate.year)
UNTIL bDate = aDate


PRINT "Press Q to quit"
DO: UNTIL INKEY$ = "Q"

CLOSECONSOLE
END



The PRINT statement is to check the bDate value is incrementing until bDate = aDate.

Not possible this way?
Title: Re: Comparing DBDATE values
Post by: Ionic Wind Support Team on August 20, 2008, 06:57:12 PM
No you can't compare one UDT with another.  Just make a quick function, using AND...

sub comparedates(DBDATE a, DBDATE b),int
return (a.day = b.day) AND (a.month = b.month) AND (a.year = b.year)
endsub

Then you can use the subroutine in your loop:

DO
   bDate.day = bDate.day + 1
   PRINT Str$(bDate.day)+"/"+Str$(bDate.month)+"/"+Str$(bDate.year)
UNTIL comparedates(aDate,bDate)

For larger UDT types you can use the C runtime function memcmp...

declare cdecl extern memcmp(pointer a, pointer b,int len),int

DO
   bDate.day = bDate.day + 1
   PRINT Str$(bDate.day)+"/"+Str$(bDate.month)+"/"+Str$(bDate.year)
UNTIL memcmp(aDate,bDate, LEN(DBDATE)) = 0

memcmp returns 0 when both are identical.  In the case of a date structure it is faster using the AND construct in the first example, as it will return quickly if the days are different.

Paul.


Title: Re: Comparing DBDATE values
Post by: Allan on August 20, 2008, 07:07:12 PM
Thanks you.

I will use that function to compare the DBDate UDT vars.