Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Delphi Exception bei Typecast (https://www.delphipraxis.net/36512-exception-bei-typecast.html)

murcielago 21. Dez 2004 07:28


Exception bei Typecast
 
Hallo Leute,

hab folgende Klassenstruktur:

Delphi-Quellcode:
IDIFControl = interface(IInterface)
// ein paar methoden
end;

TOwnTextField = class(TTntEdit, IDIFControl)
// implementierung der methoden
end;

var
  ctrl: IDIFControl;
wenn ich jetzt mit

Delphi-Quellcode:
ctrl := TOwnTextField.Create(myPanel); // myPanel kommt von wo anders... und ist nicht nil!
TControl(ctrl).Width := 5; // hier krachts!
drauf zugreife, dann kommt eine EAccessViolation. (ja, ich hab sie vorher initialisiert!)

ich muss dazusagen, dass es das erste mal ist dass ich mit delphi mit interfaces/vererbung arbeite.

lg, Lukas

Jens Schumann 21. Dez 2004 07:47

Re: Exception bei Typecast
 
Hallo,
ist crtl eine Instanz der Klasse TOwnTextField, oder reräsentiert crtl das Interface IDIFControl?

murcielago 21. Dez 2004 07:50

Re: Exception bei Typecast
 
hallo,

Delphi-Quellcode:
ctrl := TOwnTextField.Create(myLabel);
ruf ich auf.

ist also von TTntEdit abgeleitet und implementiert IDIFControl.

Jens Schumann 21. Dez 2004 07:52

Re: Exception bei Typecast
 
Hallo,
Zitat:

Zitat von murcielago
hallo,

Delphi-Quellcode:
ctrl := TOwnTextField.Create(myLabel);
ruf ich auf.

ist also von TTntEdit abgeleitet und implementiert IDIFControl.

das stimmt. Aber was ist damit
Delphi-Quellcode:
var
  ctrl: IDIFControl;

murcielago 21. Dez 2004 07:53

Re: Exception bei Typecast
 
ja, das mach ich deswegen damit ctrl alles sein kann was IDIFControl implementiert...
ich hab da ja noch TOwnCheckBox usw...

Jens Schumann 21. Dez 2004 07:57

Re: Exception bei Typecast
 
Hallo,
crtl ist das Interface und nicht die Klasse. crtl stammt nicht von TControl ab!!!
Somit muss ein Typecast auch in die Hose gehen.

murcielago 21. Dez 2004 07:58

Re: Exception bei Typecast
 
ok, dann muss ich mir was anderes einfallen lassen. danke

Christian Seehase 21. Dez 2004 12:54

Re: Exception bei Typecast
 
Moin Lukas,

ein Typecast dieser Form:

Delphi-Quellcode:
TControl(ctrl).Width ...
ist immer gefährlich.

Hättest Du es so

Delphi-Quellcode:
(ctrl as TControl).Width ...
gemacht, hätte wohl schon der Compiler gemeckert.

Im ersten Fall übernimmst Du die Verantwortung dafür, dass der Typecast korrekt ist (der Pointer also auf ein passendes Objekt zeigt), im zweiten kann der Compiler schon beim Compilieren feststellen, ob die Typen passen.

murcielago 21. Dez 2004 13:04

Re: Exception bei Typecast
 
ok, danke. habs inzwischen anders gelöst.


Alle Zeitangaben in WEZ +1. Es ist jetzt 20:07 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz