Recent

Author Topic: Question about initialisation in Class Constructor  (Read 490 times)

Wilko500

  • Full Member
  • ***
  • Posts: 130
Question about initialisation in Class Constructor
« on: February 01, 2025, 03:21:52 pm »
I have working class(s) used to hold variables that are used globally within my program.  These variable are required to persist between program runs and are loaded from an ini file during program startup.  As a sideline I am running on MacOs but using an ini file instead of XML, perhaps not ideal on Mac environment but at leat I am using the Application Support and Library Preferences file location as per good programming rules(?).  The program was migrated from VB6 and one day I'll get rid of the ini file.

I have added initialisations in the class constructor and while reviewing my code for other purposes I noticed an inconsistency. For both classes my private variables are prefixed by v_ but in the constructor code I initialise one with the private var name and the other with property name
Code: Pascal  [Select][+][-]
  1.   DisplayDate:=300000;
  2.   v_SystemName:='XX';
The compiler does not object and both work as expected. I don't recall any reason why I coded them differently but I thought the correct way was to use the private var name.  Have I been lucky?  Can someone confirm the correct/preferred method of initialisation in constructor class.


Code: Pascal  [Select][+][-]
  1.  unit U_clsGlobals;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. Type
  8.   TGlobals = Class
  9.  
  10. Private
  11.   //Timers
  12.   v_DisplayDate:              Int64;
  13.   v_PvFileScan:               Int64;
  14.   v_PvUploadStartDelay:       Int64;
  15.   v_PvUploadInt:              Int64;
  16.   v_PvUploadSlotCheck:        Int64;
  17.   v_PvErrorLogCheck:          Int64;
  18.  
  19. Public
  20.   Constructor Create();
  21.   //Timers
  22.   Property DisplayDate: Int64 Read v_DisplayDate Write v_DisplayDate;
  23.   Property PvFileScan: Int64 Read v_PvFileScan Write v_PvFileScan;
  24.   Property PvUploadStartDelay: Int64 Read v_PvUploadStartDelay Write v_PvUploadStartDelay;
  25.   Property PvUploadInt: Int64 Read v_PvUploadInt Write v_PvUploadInt;
  26.   Property PvUploadSlotCheck: Int64 Read v_PvUploadSlotCheck Write v_PvUploadSlotCheck;
  27.   Property PvErrorLogCheck: Int64 Read v_PvErrorLogCheck Write v_PvErrorLogCheck;
  28. End;
  29.  
  30. Type
  31.   TSys = Class
  32. Private
  33.   v_SystemName:    String;
  34.   v_SystemDate:    String;
  35. Public
  36.   Constructor Create();
  37.   Property SystemName: String Read v_SystemName Write v_SystemName;
  38.   Property SystemDate: String Read v_SystemDate Write v_SystemDate;
  39. End;
  40.  
  41. Implementation
  42.  
  43. Uses
  44.   Classes, SysUtils;
  45.  
  46. Constructor TGlobals.Create();
  47. Begin
  48.   //Timers
  49.   DisplayDate:=300000;
  50.   PvFileScan:=300000;
  51.   PvUploadStartDelay:=300000;
  52.   PvUploadInt:=300000;
  53.   PvUploadSlotCheck:=300000;
  54.   PvErrorLogCheck:=300000;
  55. End;
  56. Constructor TSys.Create();
  57. Begin
  58.   v_SystemName:='XX';
  59.   v_SystemDate:='XX';
  60.  
  61. End;
  62.  
  63. End.
  64.  
MacBook Pro mid 2015 with OS Monterey 12.7.6
FPC 3.2.3 Lazarus 3.7
FPC 3.2.2 Lazarus 3.4

cdbc

  • Hero Member
  • *****
  • Posts: 1933
    • http://www.cdbc.dk
Re: Question about initialisation in Class Constructor
« Reply #1 on: February 01, 2025, 03:31:51 pm »
Hi
No worries mate, you can do both without problems.
A lot of times, I have prop-setters with big side-effects, e.g.: loading a library, so it makes sense to initialize the property, which then again load a lib and initializes that, before it returns to the constructor again  :D
HTH
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Wilko500

  • Full Member
  • ***
  • Posts: 130
Re: Question about initialisation in Class Constructor
« Reply #2 on: February 01, 2025, 03:57:53 pm »
Thanks Benny. Good to know that I haven’t broken the rules.
By the way I love your strap line. One of my favourite sayings.
MacBook Pro mid 2015 with OS Monterey 12.7.6
FPC 3.2.3 Lazarus 3.7
FPC 3.2.2 Lazarus 3.4

ASerge

  • Hero Member
  • *****
  • Posts: 2389
Re: Question about initialisation in Class Constructor
« Reply #3 on: February 01, 2025, 09:48:40 pm »
Good to know that I haven’t broken the rules.
I think Benny was being sarcastic, considering the last sentence. It is better to refer to self fields inside methods, rather than properties.

Wilko500

  • Full Member
  • ***
  • Posts: 130
Re: Question about initialisation in Class Constructor
« Reply #4 on: February 01, 2025, 10:02:20 pm »
Hmmm!
It is better to refer to self fields inside methods, rather than properties.
This is what I would usually do. My question appears almost pointless now since both forms are apparently permissible. I was really asking from a point of wanting to lear/gain a better understanding.  You say it is better but is there a why?
MacBook Pro mid 2015 with OS Monterey 12.7.6
FPC 3.2.3 Lazarus 3.7
FPC 3.2.2 Lazarus 3.4

 

TinyPortal © 2005-2018