* * *

Author Topic: [SOLVED] Compare date in StringGrid  (Read 624 times)

MarcCoo

  • New member
  • *
  • Posts: 6
[SOLVED] Compare date in StringGrid
« on: September 05, 2017, 06:50:28 pm »
Hello together!

I've got a StringGrid with a few holidays, the date of the beginning in col2 and the date of the end in col3.
In the Listbox is a date (or more) and now I wanna check if the date is in the holidays.
Since three days I try to find a way, hopefully someone can help me a bit. %)

Code: Pascal  [Select]
  1. // function IsHoliday ?
  2. function TForm1.IsHoliday(holiday: string):string;
  3. var
  4.   s: string;
  5.   i: Integer;
  6.   id: integer;
  7.   D1: string;
  8.   D2: string;
  9. begin
  10.   try
  11.     for id :=0 to StringGrid2.RowCount -1 do;
  12.      begin
  13.           for i:=0 to ListBox1.Items.Count -1 do;
  14.             begin
  15.               D1:= StringGrid2.Cells[2,id];
  16.               D2:= StringGrid2.Cells[3,id];
  17.               S:= ListBox1.Items.Strings[i];
  18.  
  19.               if (StrToDate(S) >= StrToDate(D1)) and
  20.                  (StrToDate(S) <= StrToDate(D2)) then
  21.                begin
  22.                result:= 'yes holiday';
  23.                end
  24.             else
  25.             begin
  26.             end
  27.              end;
  28.          else
  29.          begin
  30.            result:= 'no :-(';
  31.       end;
  32.   finally
  33.   end;
  34.   end;            
  35.  
« Last Edit: September 06, 2017, 09:03:04 am by MarcCoo »

wp

  • Hero Member
  • *****
  • Posts: 3889
Re: Compare date in StringGrid
« Reply #1 on: September 05, 2017, 07:25:14 pm »
You don't say what's the problem.

Maybe crashing due to invalid date? This could happen if your StringGrid has a header row containing non-date strings. Since your id loop begins with 0 it tries to convert the header text to a date. Start it at 1 or, more general, at StringGrid2.FixedRows to skip the header row.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

MarcCoo

  • New member
  • *
  • Posts: 6
Re: Compare date in StringGrid
« Reply #2 on: September 05, 2017, 07:38:54 pm »
Aww...yeah true!

My Problem is, that the Code only works with the last holiday in the rows.
Sample:
Christmas 15.12.2017 - 05.01.2018
Spring 06.02.2018 - 18.02.2018
Easter 06.03.2018 - 17.03.2018

Only for Easter I get a feedback, for the other dates the code doesn't work.

wp

  • Hero Member
  • *****
  • Posts: 3889
Re: Compare date in StringGrid
« Reply #3 on: September 05, 2017, 07:40:43 pm »
Please post a sample project (only .pas, .lfm, .lpr and .lpi files packed as a common .zip which you can upload under "Attachments and other option" below the edit field).
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

MarcCoo

  • New member
  • *
  • Posts: 6
Re: Compare date in StringGrid
« Reply #4 on: September 05, 2017, 08:46:34 pm »
Hey, thank you.
Here is my little programm to calculate my busses...
the function is:

// Prozedur IstHoliday ?
function TForm1.IstRabatt
« Last Edit: September 05, 2017, 11:19:46 pm by MarcCoo »

wp

  • Hero Member
  • *****
  • Posts: 3889
Re: Compare date in StringGrid
« Reply #5 on: September 05, 2017, 09:14:53 pm »
Sorry, I'm glad to help but I am not going to take my time to debug a full application which requires me to install other thirdparty packages, is in a language which I don't understand (ok - this does not count here, I am German, but others are not) and which I don't know how to use. If you really want me to take my time to help you, you must take your time to write a little demo application which does nothing else but show the error. Usually, people find the bug by themselves along the way...
« Last Edit: September 05, 2017, 09:19:00 pm by wp »
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

MarcCoo

  • New member
  • *
  • Posts: 6
Re: Compare date in StringGrid
« Reply #6 on: September 05, 2017, 11:23:32 pm »
For sure mate!
That was a bit thoughtless from me. A clean, easy sample will come...

MarcCoo

  • New member
  • *
  • Posts: 6
Re: Compare date in StringGrid
« Reply #7 on: September 06, 2017, 12:12:26 am »
So now a bit cleaner and with no third-party-stuff   :)

Only for the dates in the last row I get the right answer.
Maybe I have to set a break as soon as something has been found? 


wp

  • Hero Member
  • *****
  • Posts: 3889
Re: Compare date in StringGrid
« Reply #8 on: September 06, 2017, 12:40:26 am »
OK, I should have seen it in your posted code above, but without the debugger I am blind...

Two issues
  • If you add a semicolon after the "do" of the "for" instruction then the loop ends exactly there, and the block below is not considered to belong to the loop.
  • You must exit the loop once a holiday has been found because if a non-holiday region follows the Edit.Caption will be reset to "no".
Another remark: There's no need for a try-finally block here. Try-finally is for resource protection since  the finally block is always executed and can be used to free resources.

Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   s: string;
  4.   i: Integer;
  5.   id: integer;
  6.   D1: string;
  7.   D2: string;
  8. begin
  9.     // Begin loop at id = 1 to skip the non-date header
  10.     for id :=1 to StringGrid1.RowCount -1 do    //  <--- NO SEMICOLON HERE
  11.      begin
  12.           for i:=0 to ListBox1.Items.Count -1 do   //  <-- NO SEMICOLON HERE
  13.             begin
  14.               D1:= StringGrid1.Cells[2,id];
  15.               D2:= StringGrid1.Cells[3,id];
  16.               S:= ListBox1.Items.Strings[i];
  17.  
  18.             if (StrToDate(S) >= StrToDate(D1)) and
  19.                (StrToDate(S) <= StrToDate(D2)) then
  20.                begin
  21.                   Edit1.Caption:= 'yes holiday!';
  22.                   exit;    // <--- Exit the procedure once a holiday is found.
  23.                end
  24.                 else
  25.                 begin
  26.                    Edit1.Caption:= 'no';
  27.                 end
  28.               end;
  29.      end;
  30.   end;
« Last Edit: September 06, 2017, 12:51:34 am by wp »
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

MarcCoo

  • New member
  • *
  • Posts: 6
Re: Compare date in StringGrid
« Reply #9 on: September 06, 2017, 09:01:38 am »
Aww! 
I never stop learning.

I tried with break and exit before, but never seen that the issue is the semicolon...
will keep that in my mind.

Thank you soo much wp!

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus