Recent

Author Topic: [SOLVED] dependency initialization problem  (Read 2921 times)

ndrx

  • Newbie
  • Posts: 4
[SOLVED] dependency initialization problem
« on: August 11, 2017, 02:22:40 pm »
Hello,

I have a problem with the initialization of a variable in a constructor of a class.

The variable is a TDateTime and I want to initialize it with value Now (from unit DateUtils) but it is not possible, the compiler is complaining  :'( :

Error: Identifier not found "now"

I know it's a basic problem but I already searched for information on the web and I don't find anything.

Thank you for your help.
« Last Edit: August 21, 2017, 10:12:28 am by ndrx »

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: dependency initialization problem
« Reply #1 on: August 11, 2017, 02:35:40 pm »
hi,
if you could share some code.

Code: Pascal  [Select][+][-]
  1. Form1.Caption:= FormatDateTime('hh',now);  

works for me.
Lazarus 2.0.2 64b on Debian LXDE 10

balazsszekely

  • Guest
Re: dependency initialization problem
« Reply #2 on: August 11, 2017, 02:37:52 pm »
@ndrx
Add sysutils to the uses clauses.

ndrx

  • Newbie
  • Posts: 4
Re: dependency initialization problem
« Reply #3 on: August 11, 2017, 02:45:23 pm »
@tudi_x
here is my code :
Code: Pascal  [Select][+][-]
  1. unit myunit;
  2. {$mode objfpc}  
  3. {$codepage UTF8}
  4.  
  5. interface
  6.  
  7. uses Classes, dateutils, sysutils;
  8.  
  9. var
  10.         INITIAL_DATE : TDateTime;
  11.  
  12. type Test1 = class
  13.         private
  14.                 time : Int64;
  15.         public
  16.                 constructor create();
  17. end;
  18.  
  19. implementation
  20.  
  21.         constructor Test1.create();
  22.         begin
  23.                 time = MilliSecondsBetween(INITIAL_DATE, now);
  24.         end;
  25.  
  26. initialization
  27. begin
  28.         INITIAL_DATE := scanDateTime('DDMMYYYY', '01011900');
  29. end;
  30.  
  31. end.
  32.  

@GetMem
I added sysutils but I have the same problem.

balazsszekely

  • Guest
Re: dependency initialization problem
« Reply #4 on: August 11, 2017, 02:52:58 pm »
No! You don't have the same problem. Before sysutils the error was: "Identifier not found: now". After you add sysutils the error is "Illegal expression". You need ":=" instead of "=".

Code: Pascal  [Select][+][-]
  1. constructor Test1.create();
  2. begin
  3.    time := MilliSecondsBetween(INITIAL_DATE, now);
  4. end;

ndrx

  • Newbie
  • Posts: 4
Re: dependency initialization problem
« Reply #5 on: August 11, 2017, 03:26:12 pm »
Sorry, you're right! It's not the same problem...

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: dependency initialization problem
« Reply #6 on: August 12, 2017, 11:18:38 am »
Nitpicking a little, but:

Code: Pascal  [Select][+][-]
  1. begin
  2.         INITIAL_DATE := scanDateTime('DDMMYYYY', '01011900');
  3. end;
  4.  

Why not

Code: Pascal  [Select][+][-]
  1.   INITIAL_DATE := EncodeDate(1900,1,1);
  2.  

Or just calculate that value once and include it as a constant in your sourcecode.

Bart

ndrx

  • Newbie
  • Posts: 4
Re: dependency initialization problem
« Reply #7 on: August 21, 2017, 10:11:47 am »
@GetMem
Ok, my problem is solved! Thank you very much!

@Bart
Thank you for your advice :-)

 

TinyPortal © 2005-2018