Autor Beitrag
FinalFantasy
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 127

Windows XP
Delphi 5 Professional, Visual Studio 7 .NET (C#)
BeitragVerfasst: Do 30.06.05 06:54 
Hi,

ich habe eine Klasse geschrieben.
Etwa so:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
TMyClass = class
public
  constructor Create(i: integer);
private 
  myint: integer;
end;

constructor TMyClass.Create(i: integer);
begin
  myint := i;
end;


So, der Konstruktor wird ganz brav aufgerfufen, jedoch bekomme ich bei der Zuweisung im Konstruktor EAccessViolation. Self ist komischer Weise auch nil.
Was mach ich denn falsch?

Nebenbei: Wie kann man hier formatierten Code schreiben? Die Buttons haben bei mir irgendwie keine Wirkung...

Moderiert von user profile iconraziel: Delphi-Tags hinzugefügt.
Softchaos67
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 103

XP Prof
Delphi 7
BeitragVerfasst: Do 30.06.05 07:56 
Wie sieht denn die Zuweisung aus? In etwa so?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
procedure MyProc;
var MyClass :TMyClass;
begin
   MyClass := TMyClass.Create(15);
   FreeAndNil(MyClass);
end;

So oder so ähnlich solltest du das verwenden, dann kanns eigentlich keine AccessViolation geben.

_________________
cum grano salis
FinalFantasy Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 127

Windows XP
Delphi 5 Professional, Visual Studio 7 .NET (C#)
BeitragVerfasst: Do 30.06.05 07:59 
Ja, genau so sieht es aus... geht aber trotzdem nicht... Ich versteh das nicht.
Softchaos67
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 103

XP Prof
Delphi 7
BeitragVerfasst: Do 30.06.05 08:00 
poste mal etwas source, vielleich kann man da mehr sehen...

_________________
cum grano salis
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Do 30.06.05 08:14 
Du rufst den ererbten Konstruktor der Elternklasse nicht auf!

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
constructor Create(i: Integer);
begin
  inherited Create(AOwner);
  //Rest...
end;


AXMD
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 30.06.05 08:16 
user profile iconAXMD hat folgendes geschrieben:
Du rufst den ererbten Konstruktor der Elternklasse nicht auf!
Der, da von TOBject abgeleitet wird, aber keinen Parameter AOwner erhält, sondern keinen Parameter erhält ;-)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Softchaos67
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 103

XP Prof
Delphi 7
BeitragVerfasst: Do 30.06.05 08:21 
braucht er auch nicht!
hab mal ein Beispiel gemacht, funktioniert wunderbar!
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
unit Unit1;
interface

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

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

type TMyClass=class
public
   constructor Create(i :integer);
private
   myInt :Integer;
end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TMyClass }

constructor TMyClass.Create(i: integer);
begin
   myInt := i;
   ShowMessage(IntToStr(myInt));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  myClass :TMyClass;
begin
   myClass := TMyClass.Create(15);
   FreeAndNil(MyClass);

end;
end.

_________________
cum grano salis
FinalFantasy Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 127

Windows XP
Delphi 5 Professional, Visual Studio 7 .NET (C#)
BeitragVerfasst: Do 30.06.05 08:21 
Ich leite doch gar nicht ab.... oder macht das Delphi dann standardmäßig von TObject?

PS: Ich bin mit Delphi 5 unterwegs... vielleicht macht das auch was aus.


Zuletzt bearbeitet von FinalFantasy am Do 30.06.05 08:23, insgesamt 1-mal bearbeitet
Softchaos67
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 103

XP Prof
Delphi 7
BeitragVerfasst: Do 30.06.05 08:22 
Delphi leitet immer von TObject ab, auch wenn man hinter Class nichts angibt!

_________________
cum grano salis
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 30.06.05 08:25 
user profile iconSoftchaos67 hat folgendes geschrieben:
braucht er auch nicht!
hab mal ein Beispiel gemacht, funktioniert wunderbar!
Japp. Unter Delphi Win32 (und darum gehr es hier wohl), ruft Delphi den von TObject geerbten Konstruktor IIRC implizit selber auf.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
FinalFantasy Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 127

Windows XP
Delphi 5 Professional, Visual Studio 7 .NET (C#)
BeitragVerfasst: Do 30.06.05 08:37 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
procedure test();
var myclass: TMyClass;
begin
  myclass.Create(10);
end;


Beim Aufruf von Create bekomme ich jetzt den Hinweis "[Warnung] uPicDetail.pas(90): Variable 'myclass' wurde wahrscheinlich nicht Initialisiert"... aber genau das sollte doch der Constructor machen.
Wenn ich in Create explizit den Constructor von TObject aufrufe ändert das auch nichts.

Moderiert von user profile iconChristian S.: Delphi-Tag korrigiert
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 30.06.05 08:40 
Hallo!

Lies Dir nochmal genau durch, wie Softchaos67 in seinem ersten POsting den Konstruktor aufruft! Du wirst einen kleinen, aber feinen Unterschied zu Deinem Quelltext feststellen.

Grüße
Christian

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Softchaos67
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 103

XP Prof
Delphi 7
BeitragVerfasst: Do 30.06.05 08:42 
Ich wollte es ja nicht vorhersagen, aber genau das hab ich vermutet!

Gruss

_________________
cum grano salis
FinalFantasy Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 127

Windows XP
Delphi 5 Professional, Visual Studio 7 .NET (C#)
BeitragVerfasst: Do 30.06.05 08:43 
Hmpf... danach habe ich jetzt 4 Stunden lang gesucht....
Manchmal sieht man den Wald vor lauter Bäumen nicht...
Dank, dass du mir die Augen geöffnet hast :-)
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Do 30.06.05 08:50 
user profile iconFinalFantasy hat folgendes geschrieben:
Nebenbei: Wie kann man hier formatierten Code schreiben? Die Buttons haben bei mir irgendwie keine Wirkung...

Um auch diese Frage zu beantworten :wink: : Wahrscheinlich steht bei dir (wenn du den IE benutzt) ganz links unten ein Ausrufezeichen und daneben "Es ist ein Fehler aufgetreten", deshalb gehen die Buttons nicht.

Entweder ist das nach dem Neuladen der Seite (F5) in Ordnung, oder du musst selbst die Tags hinschreiben:
[ delphi ]
//Dein Quelltext
[ /delphi ]
(Nur natürlich ohne die Leerzeichen in den Klammern)
FinalFantasy Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 127

Windows XP
Delphi 5 Professional, Visual Studio 7 .NET (C#)
BeitragVerfasst: Do 30.06.05 09:06 
Mhh, war ein Anfängerfehler, aber die stehen mir ja (noch) zu :-)
Vielleicht sieht man an dem Fehler, dass ich eher aus der C++, Java und PHP Ecke komme... da funktioniert das ganze Klassenkonzept ein wenig anders.

Also zu dem Code:
Ich benutze Opera, und nicht den IE. Allerdings bin ich auch schon auf die Idee gekommen, dass es sich um einen Javascriptfehler oder ähnliches handeln könnte, allerdings zeigt mir der IE auch keinen Fehler an.
Naja, egal, jetzt weiss ich ja, wies geht, zumindest mit dem "Tag selbst eingeben".