Forum > Beginners

property write method implement go sigsevg error

(1/2) > >>

vkhoa:
this is my code of unit1, contain form, place a label1,edit1,button1
it thrown sigsevg error at line "if not (txt=avalue) then"
if comment it then other sigsevg error occur at line 403b00

--- Code: ---unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }
  valueeventhandler =procedure (newvalue:string) of object;
  type varb=class(tobject)
     procedure setvalue(avalue:string);
     function getvalue:string;
  private
    txt:string;
    valchange:valueeventhandler;
  public
     property value:string read getvalue write setvalue;
     property valuechange:valueeventhandler read valchange write valchange;
  end ;
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure varbchange(txt:string) ;//of object;
  private
    { private declarations }
  public
    { public declarations }
    myvar:varb;
  end;


var
  Form1: TForm1;

implementation

{$R *.lfm}

    { TForm1 }

    procedure TForm1.FormCreate(Sender: TObject);
    begin
      myvar.value:='is first value and assign manual to label,then add value change event';
      label1.Caption:=myvar.value;
      myvar.valuechange:=@varbchange;
    end;
    procedure tform1.varbchange(txt:string);
    begin
        label1.caption:=myvar.value;
    end;

    procedure varb.setvalue(avalue:string);
    begin
         if not (txt=avalue) then//<-------------------------------error at this line
          begin
          txt:=avalue;
          if assigned(valuechange) then
             valuechange(avalue);
          end;
    end;
    function varb.getvalue:string;
    begin
        result:=txt;
    end;
end.
                             
--- End code ---
please help

Blaazen:
Because you never created "myvar".
You need to add somewhere:

--- Code: ---myvar:=Varb.Create;
--- End code ---

taazz:

--- Quote from: vkhoa on May 26, 2014, 01:11:15 pm ---this is my code of unit1, contain form, place a label1,edit1,button1
it thrown sigsevg error at line "if not (txt=avalue) then"
if comment it then other sigsevg error occur at line 403b00
please help

--- End quote ---


--- Code: ---unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }
  valueeventhandler =procedure (newvalue:string) of object;
  type varb=class(tobject)
     procedure setvalue(avalue:string);
     function getvalue:string;
  private
    txt:string;
    valchange:valueeventhandler;
  public
     property value:string read getvalue write setvalue;
     property valuechange:valueeventhandler read valchange write valchange;
  end ;
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure varbchange(txt:string) ;//of object;
  private
    { private declarations }
  public
    { public declarations }
    myvar:varb;
    constructor Create(aOwner:Tcomponent);override;//<-------------------------------Fix
  end;


var
  Form1: TForm1;

implementation

{$R *.lfm}

    { TForm1 }

    constructor TForm1.Create(aOwner:Tcomponent);override;
    begin
        Inherited Create(aOwner);
        myvar := varb.Create;//<-------------------------------Fixed,
    end;
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      myvar.value:='is first value and assign manual to label,then add value change event';
      label1.Caption:=myvar.value;
      myvar.valuechange:=@varbchange;
    end;
    procedure tform1.varbchange(txt:string);
    begin
        label1.caption:=myvar.value;
    end;

    procedure varb.setvalue(avalue:string);
    begin
         if not (txt=avalue) then
          begin
          txt:=avalue;
          if assigned(valuechange) then
             valuechange(avalue);
          end;
    end;
    function varb.getvalue:string;
    begin
        result:=txt;
    end;
end.
                             
--- End code ---

vkhoa:
thanks blaazen so much
I forgot doing that, perhap all class must be created before using

Leledumbo:

--- Quote ---I forgot doing that, perhap all class must be created before using
--- End quote ---
Except for class var, class const, class type and class method, you must create the instance first.

Navigation

[0] Message Index

[#] Next page

Go to full version