Using create new should invoke constructor? lazarus / 31-LCL ---------------------------------------------------------------------- bdexterholland · 2018-01-04 20:32 Hi guys, I`m on a situation that create a from by scratch seems the better option. By the way, I faced the situation that I need to call TForm.CreateNew. So, here is my question: Should CreateNew calls the constructor I wrote for my form or not? Here, my constructor was not called... ---------------------------------------------------------------------- Thaddy · 2018-01-04 20:43 Well, in your overloaded constructor you should always call the inherited constructor. If you don't the inherited constructor will not be called. And that will screw things up. ---------------------------------------------------------------------- bdexterholland · 2018-01-04 20:46 Hi Thaddy, Thanks for the reply Neither my overloaded constructor was called when I used CreateNew. Since it`s my first time using this method, I don't know the default behavior. ---------------------------------------------------------------------- taazz · 2018-01-04 20:47 provide some code for us to see what you mean by "your constructor". In short I do not use createnew I set the variable forms.RequireDerivedFormResource to false when required. ---------------------------------------------------------------------- bdexterholland · 2018-01-04 21:03 Sure, I didn't use RequireDerivedFormResource. I Saw that hint on Laz 1.0 Release notes (link)... here is my code Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit unt_dialogo; {$mode objfpc}{$H+} interface type  TTipoDialogo = (dtInformacao, dtAviso, dtErro, dtPergunta);  TRetornoDialogo = (rdOK, rdSim, rdNao, rdCancelar); function ShowDialog(const AMessage: string;  const ATipoDialog: TTipoDialogo = dtInformacao;  const Login: string = ''): TRetornoDialogo; implementation   uses  Forms, Graphics, Classes, Controls, Buttons, StdCtrls, ExtCtrls, SysUtils; const  _PN_TITULO: integer = $00DF9254;  _PN_CORPO: integer = $00F9F9F9;  _PN_RODAPE: integer = $00EFEFEF;  _LB_TITULO: integer = $00FFFFFF; type   { TDialogForm }   TDialogForm = class(TForm)  private    _Type: TTipoDialogo;    _Message: string;    _User: string;    _System: string;    _CodigoErro: string;     _iconeDialogo: TImage;    _headerPanel: TPanel;    _bodyPanel: TPanel;    _footerPanel: TPanel;    _titleLabel: TLabel;    _messageLabel: TLabel;    _okButton: TBitBtn;    _cancelButton: TBitBtn;    _yesButton: TBitBtn;    _noButton: TBitBtn;     procedure setYesNoButtons;    procedure setOkButton;    procedure setOkCancelButtons;     procedure setDTInformacao;    procedure setDTAViso;    procedure setDTErro;    procedure setDTPergunta;     procedure PYesButton(const Sender: TObject);    procedure PNoButton(const Sender: TObject);    procedure PCancelButton(const Sender: TObject);    procedure POkButton(const Sender: TObject);   public    constructor Create(const ASistema: string = 'NAO INFORMADO');    function ShowModal(const Atipo: TTipoDialogo; const AMensagem: string; const AUsuario: string = 'NÃO INFORMADO'): TModalResult;                procedure InicializarSubobjetos;  end; { TDialogForm }var  DialogForm: TDialogForm; function ShowDialog(const AMessage: string; const ATipoDialog: TTipoDialogo;  const Login: string): TRetornoDialogo;begin        if not(Assigned(DialogForm)) then    begin            DialogForm := TDialogForm.CreateNew(Nil);    DialogForm.InicializarSubobjetos;    end;   case DialogForm.ShowModal(ATipoDialog, AMessage, Login) of    mrYes: Result := rdSim;    mrNo: Result := rdNao;    mrCancel: Result := rdCancelar;    else      Result := rdOK;  end;end; procedure TDialogForm.setYesNoButtons;begin  _yesButton.Visible := True;  _noButton.Visible := True;  _cancelButton.Visible := False;  _okButton.Visible := False;end; procedure TDialogForm.setOkButton;begin  _yesButton.Visible := False;  _noButton.Visible := False;  _cancelButton.Visible := False;  _okButton.Visible := True;end; procedure TDialogForm.setOkCancelButtons;begin  _yesButton.Visible := False;  _noButton.Visible := False;  _cancelButton.Visible := True;  _okButton.Visible := True;end; constructor TDialogForm.Create(const ASistema: string);begin  Self._System := ASistema;end; function TDialogForm.ShowModal(const Atipo: TTipoDialogo; const AMensagem: string;  const AUsuario: string): TModalResult;begin  case Atipo of  dtInformacao: Self.setDTInformacao;    dtAviso: Self.setDTAViso;    dtErro: Self.setDTErro;    dtPergunta: Self.setDTPergunta;  end;   Self._Message := AMensagem;  Self._messageLabel.Caption := AMensagem;  Self._User := AUsuario;   Result := inherited ShowModal;   Self._Message := '';  Self._User := ''; end; procedure TDialogForm.InicializarSubobjetos;begin  {$REGION 'Formulário'}  Self.Width := 450;  Self.Height := 300;  Self.Position := poScreenCenter;  Self.BorderIcons := [];  Self.BorderStyle := bsSingle;  Self.Font.Size := 12;{$ENDREGION} {$REGION 'Paineis'}  _headerPanel := TPanel.Create(Self);        _headerPanel.Parent := Self;  _headerPanel.Align := alTop;  _headerPanel.Color := _PN_TITULO;  _headerPanel.BevelOuter := bvNone;  _headerPanel.BorderWidth := 10;  _headerPanel.Height := 50;   _bodyPanel := TPanel.Create(Self);  _bodyPanel.Parent := Self;  _bodyPanel.Align := alClient;  _bodyPanel.Color := _PN_CORPO;  _bodyPanel.BevelOuter := bvNone;  _bodyPanel.BorderWidth := 10;   _footerPanel := TPanel.Create(Self);  _footerPanel.Parent := Self;  _footerPanel.Align := alBottom;  _footerPanel.Color := _PN_RODAPE;  _footerPanel.BevelOuter := bvNone;  _footerPanel.BorderWidth := 10;  _footerPanel.Height := 55;{$ENDREGION} {$REGION 'Labels'}  _titleLabel := TLabel.Create(_headerPanel);  _titleLabel.Parent := _headerPanel;  _titleLabel.Font.Size := 16;  _titleLabel.Font.Color := clWhite;  _titleLabel.Align := alClient;  _titleLabel.Layout := tlCenter;  _titleLabel.Caption := 'Título da janela';  _titleLabel.BorderSpacing.Left := 10;   _messageLabel := TLabel.Create(_bodyPanel);  _messageLabel.Parent := _bodyPanel;  _messageLabel.Font.Color := clWindowText;  _messageLabel.Align := alLeft;  _messageLabel.Layout := tlTop;  _messageLabel.Caption := 'Texto da mensagem aparecerá aqui...';{$ENDREGION} {$REGION 'Botões'}  {TODO: Carregar ícones dos botões }  _yesButton := TBitBtn.Create(Self);        _yesButton.Parent := _footerPanel;  _yesButton.Align := alRight;  _yesButton.Caption := 'Sim';  _yesButton.Width := 100;  _yesButton.OnClick := @PYesButton;  _yesButton.BorderSpacing.Left := 10;   _noButton := TBitBtn.Create(Self);  _noButton.Parent := _footerPanel;  _noButton.Align := alRight;  _noButton.Caption := 'Não';  _noButton.Width := 100;  _noButton.OnClick := @PNoButton;  _noButton.BorderSpacing.Left := 10;   _okButton := TBitBtn.Create(Self);  _okButton.Parent := _footerPanel;  _okButton.Align := alRight;  _okButton.Caption := 'OK';  _okButton.Width := 100;  _okButton.OnClick := @POkButton;  _okButton.BorderSpacing.Left := 10;   _cancelButton := TBitBtn.Create(Self);  _cancelButton.Parent := _footerPanel;  _cancelButton.Align := alRight;  _cancelButton.Caption := 'Cancelar';  _cancelButton.Width := 100;  _cancelButton.OnClick := @PCancelButton;  _cancelButton.BorderSpacing.Left := 10;{$ENDREGION} {$REGION 'Icone da janela'}  {TODO: Adicionar ícone de acordo com o tipo de diálogo}  _iconeDialogo := TImage.Create(_headerPanel);  _iconeDialogo.Parent := _headerPanel;  _iconeDialogo.Align := alLeft;  _iconeDialogo.Width := 32;{$ENDREGION}end; procedure TDialogForm.setDTInformacao;begin  Self._titleLabel.Caption := 'Informação';  Self.Caption := Self._titleLabel.Caption;  Self.setOkButton;end; procedure TDialogForm.setDTAViso;begin  Self._titleLabel.Caption := 'Aviso';  Self.Caption := Self._titleLabel.Caption;  Self.setOkButton;end; procedure TDialogForm.setDTErro;begin  Self._titleLabel.Caption := 'Erro';  Self.Caption := Self._titleLabel.Caption;  Self.setOkButton;end; procedure TDialogForm.setDTPergunta;begin  Self._titleLabel.Caption := 'Pergunta';  Self.Caption := Self._titleLabel.Caption;  Self.setYesNoButtons;end; procedure TDialogForm.PYesButton(const Sender: TObject);begin  Self.ModalResult := mrYes;end; procedure TDialogForm.PNoButton(const Sender: TObject);begin  Self.ModalResult := mrNo;end; procedure TDialogForm.PCancelButton(const Sender: TObject);begin  Self.ModalResult := mrCancel;end; procedure TDialogForm.POkButton(const Sender: TObject);begin  Self.ModalResult := mrOk;end; finalization  DialogForm.Free; end. ---------------------------------------------------------------------- taazz · 2018-01-04 21:10 two comments 1) you do not have any overriden constructor. You need to actually define override, you might have an overloaded one but it will never be called automatically. 2) call your overloaded constructor just make sure that your constructor calls the inherited createnew constructor. I have no idea which hint you refer to. ---------------------------------------------------------------------- bdexterholland · 2018-01-04 21:13 Quote1) you do not have any overriden constructor. You need to actually define override, you might have an overloaded one but it will never be called automatically. oh fuck, was it that i forgot? Beginner miskate, sorry.... Anyway, CreateNew should call default constructor? ---------------------------------------------------------------------- taazz · 2018-01-04 21:16 Quote from: bdexterholland on January 04, 2018, 10:13:58 pmQuote1) you do not have any overriden constructor. You need to actually define override, you might have an overloaded one but it will never be called automatically. oh fuck, was it that i forgot? Beginner miskate, sorry.... Anyway, CreateNew should call default constructor? no, not that I can see. the create constructor calls the createnew constructor internally. As I said keep your constructor overloaded (not overriden) just make sure that you call createnew inside it and simple use it. ---------------------------------------------------------------------- bdexterholland · 2018-01-04 21:17 ok, thanks ---------------------------------------------------------------------- howardpc · 2018-01-04 22:03 A couple of minor points. If you remove the global DialogForm variable (and the finalization section would not then be needed) you could write your ShowDialog procedure as follows: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function ShowDialog(const AMessage: string; const ATipoDialog: TTipoDialogo;  const Login: string): TRetornoDialogo;var  DialogForm: TDialogForm;begin  DialogForm := TDialogForm.CreateNew(Nil);  try    DialogForm.InicializarSubobjetos;     case DialogForm.ShowModal(ATipoDialog, AMessage, Login) of      mrYes: Result := rdSim;      mrNo: Result := rdNao;      mrCancel: Result := rdCancelar;      else        Result := rdOK;    end;  finally    DialogForm.Free;  end;end; This has the advantage that resources used by the dialog are returned to the system for reuse as soon as the dialog closes. Otherwise the resources taken by the dialog continue to take up memory until your app finishes. You don't need your TDialogForm.Create() constructor at all. It could be removed (along with one or two unused TFormDialog private fields). ---------------------------------------------------------------------- bdexterholland · 2018-01-06 00:36 Thanks howardpc Nice tips about resource usage. I thougth something like this but I don't know why i got stuck on some point. Some private fields are there for future implementation, like autosubmit error report when ATipoDialog = dtErro