Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Delphi Variable Declarations/Initializations

As it pertains to Delphi...

When a variable is declare of a certain type, is it initialized to an OBJECT of that type? Or must the variable be assigned an expression which returns an object of that type?

I'm coming from a strong Java background. What I mean to ask is this... In Java, say you declare an instance variable of a user defined type named Orange. Which would look like this:

private Orange _fruit;

The variable _fruit still holds a reference to null until actually assigned an instance of the Orange class, like this:

_fruit = new Orange();

In Delphi if I declare a variable of type TForm, like this:

var
  Form : TForm;

Is Form initlized to a TForm object? Or is it still nil?

I'm asking because I'm getting an error when trying to compile a small bit of code which is showen below:

Here is the Main unit:

unit Main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils,
  System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Second;

type
  TForm1 = class(TForm)
    ShowForm2: TButton;
    procedure ShowForm2Click(Sender: TObject);
  end;

var
  Form1: TForm1;
  SecondForm : TSecondForm;

implementation

{$R *.dfm}

procedure TForm1.ShowForm2Click(Sender: TObject);
begin
SecondForm.ShowModal;
end;
end.

and here is the Second unit:

unit Second;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls;

type
  TSecondForm = class(TForm)
    Label1: TLabel;
  end;

var
  SecondForm: TSecondForm;

implementation

{$R *.dfm}

end.

The error I'm getting when I try to compile is exactly: "Access violation at address 005B17F9 in module 'Multiple.exe'. Read of address 00000000." I was thinking that it's because I don't somehow initialize the variable SecondForm in unit Main? However, I tried to place 'SecondForm.Create' in the ShowForm2Click procedure and I get the same error. Am I get this error because SecondForm is unassigned? Does it need to be initialized? Or is it?

Note: I'm three days new to Delphi. Please be considerate of that.

like image 929
le-a Avatar asked Dec 21 '22 05:12

le-a


1 Answers

SecondForm.Create is the wrong syntax. Constructors are special in Delphi. You can think of them more or less like class methods. The way you invoke them is like this:

variable := ClassType.Create(arguments);

While it is possible to invoke a constructor like an instance method (variable.Create) this is for one specific use case and should not be done in general code. The reason to call a constructor on an object and not a type is if you're already inside a constructor for that object. (ie. if you have more than one constructor on the object and one of them calls another one, or to initialize the ancestor class's members by calling a constructor on the parent class with inherited Create(arguments);)

What you did, calling a constructor on an object when not inside another constructor for that object, should probably raise a compiler warning, if not an error, but unfortunately it doesn't.

like image 84
Mason Wheeler Avatar answered Dec 24 '22 01:12

Mason Wheeler