[Gambas-user] Using Date datatype for FOR-NEXT

ron ronstk at ...239...
Fri Jun 15 15:47:24 CEST 2007


On Friday 15 June 2007 08:51, Rolf-Werner Eilert wrote:
> Hope this gets through to you, I just read about trouble on the list...
> 
> When I have something like
> 
> IF mydate = Date(Now) + 1 THEN
> 
> this runs well (saying "tomorrow"), but when I have the variables
> 
> startdate, enddate, i
> 
> all of type Date, I cannot make a FOR NEXT loop with them:
> 
> FOR i = startdate to enddate
> 
> It results in errors about wrong datatype ("expected numeric, got date 
> instead" etc.)
> 
> Why's that?
> 
> Rolf


For the simple 'FOR i = startval to endval' all the variables are numerical.

With 'foreach i = startvar to endval' it could be a any datatype but the
increment must be also of that datatype. 
So it must changed to 'foreach i = startvar to endval step oneday' in that case.
However gambas does not support this way of working. :(

Another could be 'foreach i in mydatatype_array' and here i is of same datatype 
as the mydatatype_array. Just also not as your requirement with an array.

In other words, the example you give can't be done that way.

FOR i = CINT(startdate) TO CINT(enddate) step CINT( (DAY(Now)+1)-DAY(Now) )
The step value calculate to the integer value need for 1 day.

The variable i is of course integer here and if you need it as date
then CDATE(i) should be used. This is the integer variant of your example.
  note:
  This is not typed as a working example (or even tested with gambas) but to 
  explain/show how it could be done (for VB this worked for me in the past)

A more appropriate way will be something like next.
  DIM i AS Date
  i=startdate
  DO
    'your code what must be done with i
    ...
    i=i+1 ' same as DATE(Now)+1
  LOOP UNTIL i=enddate ' or enddate+1

Hope this explains something.

Ron












 
 






More information about the User mailing list