Recent

Author Topic: [Solved] - My First Class - Access violation when setting private var  (Read 1234 times)

Wilko500

  • Full Member
  • ***
  • Posts: 180
My first (failed) attempt at understanding and creating a class having read the Class documentation. My code compiles but on running in Lazarus IDE fails on any attempt to reference a private var, a Sigsegv error, when running without IDE Access Violation error. Please can someone tell me where I am going wrong.

My Class Unit
Code: Pascal  [Select][+][-]
  1. unit ClsChartSettings;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. Type
  8.     TChSet = Class
  9. Private
  10.     PageHt:    Single;   //Private var to hold Page Height
  11.  
  12. Procedure SetHt(PgHt: Single);  //Private proc to set Page Height
  13.  
  14. Public
  15.  
  16. constructor Create(ChId : String);
  17. Property PageH: Single Read PageHt Write SetHt;   //Public property read from private var
  18.                                                   //Write with proc SetHt
  19. End;
  20.  
  21. implementation
  22. uses
  23.     Classes, SysUtils, Dialogs;
  24.  
  25. constructor TChSet.Create(ChId: String);
  26. //Initialise class variable
  27. begin
  28. PageHt:=0;
  29. ShowMessage('Class Created ' + ChId);
  30. End;
  31.  
  32. Procedure TChSet.SetHt(PgHt: Single);
  33. //This proc to set private var
  34. Begin
  35. PageHt:=PgHt;
  36. End;
  37.  
  38. end.
  39.  
My Test program
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   ClsChartSettings;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Edit1: TEdit;
  18.     Edit2: TEdit;
  19.     Edit3: TEdit;
  20.     Edit4: TEdit;
  21.     Label1: TLabel;
  22.     Label2: TLabel;
  23.     Label3: TLabel;
  24.     Label4: TLabel;
  25.     Label5: TLabel;
  26.     procedure Button1Click(Sender: TObject);
  27.   private
  28.  
  29.   public
  30.  
  31.   end;
  32.  
  33. var
  34.   Form1: TForm1;
  35.  
  36. implementation
  37.  
  38. {$R *.lfm}
  39.  
  40. { TForm1 }
  41.  
  42. procedure TForm1.Button1Click(Sender: TObject);
  43. var
  44.   x:      TChSet;
  45. begin
  46.   x.Create('Test');
  47.   x.PageH:=297;
  48.   ShowMessage(x.PageH.ToString);
  49. end;
  50.  
  51. end.
  52.  
« Last Edit: February 10, 2021, 01:53:23 am by Wilko500 »
MacBook Pro mid 2015 with OS Monterey 12.7.6
FPC 3.2.3 Lazarus 3.7
FPC 3.2.2 Lazarus 3.4

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: My First Class - Access violation when setting private var
« Reply #1 on: February 10, 2021, 01:15:22 am »
Code: Pascal  [Select][+][-]
  1. var
  2.   x:      TChSet;
  3. begin
  4.   x:=TChSet.Create('Test');
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

dseligo

  • Hero Member
  • *****
  • Posts: 1683
Re: My First Class - Access violation when setting private var
« Reply #2 on: February 10, 2021, 01:27:06 am »
And put this at the end:
Code: Pascal  [Select][+][-]
  1. x.Free;

Wilko500

  • Full Member
  • ***
  • Posts: 180
Re: My First Class - Access violation when setting private var
« Reply #3 on: February 10, 2021, 01:52:59 am »
Well that was easy.  Thanks.  I should have spotted that but evidently couldn't see for looking :(. Thanks also for noting the need for x.Free. I was aware but lazy coding while focussed on the error.
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