Discussion:
Why does this NOT FAIL?
(too old to reply)
Ephraim
2008-05-27 15:16:59 UTC
Permalink
RAD 2007 Teial

I have not created an instance of DataModule2nor aut-=creation, yet no AV
when I press the button TForm1.Button1Click(Sender: TObject);
Is this code treated as a class procedure? Why does it not fail?

TIA

Ephraim


Project Source

program Project1;

uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {DataModule2: TDataModule};

{$R *.res}

begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.

I create this form

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls{, Project2_TLB};

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses Unit2;


{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
DataModule2.ShowMe('Poor Chelsea');
end;

procedure TForm1.Button2Click(Sender: TObject);
//var
// IDispl: IDisplay;
begin
// IDispl:=CoDisplay.Create;
// IDispl.DisplayMessage('This works')
end;

end.
and this DataModule

unit Unit2;

interface

uses
SysUtils, Classes, Dialogs;

type
TDataModule2 = class(TDataModule)
private
{ Private declarations }
public
{ Public declarations }
procedure ShowMe(const Text: String);
end;

var
DataModule2: TDataModule2;

implementation

{$R *.dfm}

{ TDataModule2 }

procedure TDataModule2.ShowMe(const Text: String);
begin
ShowMessage(Text)
end;

end.
Peter Below (TeamB)
2008-05-27 18:31:39 UTC
Permalink
Post by Ephraim
RAD 2007 Teial
I have not created an instance of DataModule2nor aut-=creation, yet
no AV when I press the button TForm1.Button1Click(Sender: TObject);
Is this code treated as a class procedure? Why does it not fail?
procedure TDataModule2.ShowMe(const Text: String);
begin
ShowMessage(Text)
end;
It does not fail because this method does not make any use of the Self
parameter, the datamodules address. This it is not accessed its value
is not important. As soon as you tried to access a field of the
datamodule or call a virtual method it would blow up with an access
violation.
--
Peter Below (TeamB)
Don't be a vampire (http://slash7.com/pages/vampires),
use the newsgroup archives :
http://www.tamaracka.com/search.htm
http://groups.google.com
Remy Lebeau (TeamB)
2008-05-27 21:55:59 UTC
Permalink
Post by Ephraim
Why does it not fail?
Because you are not actually accessing any of the DataModule's members
inside of ShowMe(). When you call a class method via an invalid object
pointer, no error occurs until the Self pointer inside the method is
actually used.


Gambit

Loading...