Forum > Russian

External: SIGSEGV

(1/2) > >>

Zeno:
Добрый день!
Помогите разобраться. Почему данный простой код не работает как положено.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Label1: TLabel;    procedure Button1Click(Sender: TObject);  private   public   end;  Number = Class    nb: double;  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);var  N1: Number;beginN1.nb:= (120.341154260366);label1.caption:= floattostr(N1.nb);end; end.          Если сделать nb обычной глобальной переменной то код работает. А если сделать nb переменной  класса - не работает. Объясните, пожалуйста, почему и покажите как нужно делать, так чтобы все работало. Я новичок, только изучаю ООП.

Спасибо!

Handoko:
You did it wrong.

That should be like this:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Label1: TLabel;    procedure Button1Click(Sender: TObject);  private   public   end;   { TNumber }   TNumber = class  private    FNB: Double;  public    constructor Create;    property NB: Double read FNB write FNB;  end; var  Form1: TForm1; implementation {$R *.lfm} { TNumber } constructor TNumber.Create;begin  FNB := 0;end; { TForm1 } procedure TForm1.Button1Click(Sender: TObject);var  N1: TNumber;begin  N1    := TNumber.Create;  N1.NB := 120.341154260366;  Label1.Caption := FloatToStr(N1.NB);  N1.Freeend; end.

* Usually a class has a private section (see line #27) and a public section (see line #29), read more https://wiki.freepascal.org/Class#Scope_modifiers
* Class name usually started with an uppercase letter T, so that class should be named TNumber (see line #26).
* You put variables in the private section and the name usually started with an uppercase letter F, so that variable should be named FNB (see line #28).
* In the point #2 above, the letter T means type and in the point #3, the letter F means field.
* You set the default values inside a constructor, which usually named as Create (see lines #30, #43, #45).
* To let users to access the value, you create a property (see line #31).
* You must call the constructor before you can use the class (see line #54).
* Usually you have to call Free if you no longer need it (see line #57), but there are some classes do not always require you to free them manually, for examples: TButton, TImage, etc.

WooBean:
@Zeno

Less typing solution:
change your Number class definition to

--- Quote ---Number = Class
 class var nb: double;
end;
--- End quote ---

... and your code works OK.

However, a lot reading obout OOP instead of you.

Sieben:

--- Quote ---8. Usually you have to call Free if you no longer need it (see line #57), but there are some classes do not require you to free them manually, for examples: TButton, TImage, etc.
--- End quote ---

Not quite. You don't need to explicitely free an instance when it's Create takes an Owner as parameter and this parameter is not nil, because then the Owner is responsible for freeing it. This is true eg for any component like a TButton or TImage being dropped on a form at design time, but if you have sth like


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  MyButton := TButton.Create(nil);
in your code you will have to free MyButton manually as well. Sure you know that.

Handoko:
Please don't focus on me. Think about the OP, he's just an beginner. You all know, OOP cannot be explained using some paragraphs only. If you try to include class var, ownership, etc ... that only makes the OP harder to learn OOP.

Navigation

[0] Message Index

[#] Next page

Go to full version