Autor Beitrag
fidionael
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 232

Win XP SP2, Ubuntu 6.06
Delphi 7 PE, Delphi 3 Prof
BeitragVerfasst: Do 23.02.06 14:21 
Hallöchen!

Ich habe ein kleines Problem bei der OOP in Delphi - ich habe hierzu in der Suche nichts gefunden und auch in diversen OOP Tutorials keine Antwort erhalten...

Ich habe mit OOP gerade erst angefangen und bis jetzt noch kein lauffähiges OOP-Programm zustande gebracht, da ich, sobald ich irgendwie auf Variablen zugreifen will eine Schutzverletzungsmeldung von Windows bekomme und das Programm abstürzt.

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:
unit UStatistik;

interface

type
  TStatistik = class
    private
      Messwert: Array[1..10000] of Integer;
      Anzahl: Integer;
      function GetAnzahl : Integer;
    public
      constructor Create;
      procedure Add(value: Integer);
      procedure Sort;
      function GetMin : Integer;
      function GetMax : Integer;
      function GetMedium : Real;
      function GetValue(index: Integer) : Integer;
      property Count: Integer read GetAnzahl;
  end;

implementation

constructor TStatistik.Create;
begin
     Anzahl:=0;
end;

[...]

end.


In meiner Hauptunit definiere ich Statistik: TStatistik als globale Variable und versuche diese mit dem FormCreate-Event zu "createn"... Nun bekomme ich bei Programmstart folgende Fehlermeldung:
Project OOP.exe raised exception class EAccessViolation with message 'Access violation at address 00428B12 in module 'OOP.exe' ...

Kann mir jemand helfen? Hab das Gefühl das es ein sehr trivialer Fehler ist - finde ihn aber nicht und stehe am Rande des Nervenzusammenbruches...

Danke schonmal im vorraus.
Mfg
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Do 23.02.06 14:31 
Wie erzeugts du die Variable? Da macht man oft was falsch. Richtig ist es so:
ausblenden Delphi-Quelltext
1:
2:
Statistik := tStatistik.Create;
//oft falsch: Statistik.Create;
Deweiteren fehlt im Construktor das inherited
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
constructor TStatistik.Create;   
begin   
     inherited;
     Anzahl:=0;   
end;
(obwohl ich grade nicht weiß, ob das evtl. bei Klassen ohne explizit angegebenen Vorfahr-Typ auch implizit aufgerufen wird...)

_________________
We are, we were and will not be.
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: Do 23.02.06 14:35 
Zeig mal deinen Code aus FormCreate wo du das Objekt erzeugst..

Edit: 4 Minuten zu spät? das Posting war doch grad noch nicht da.. :gruebel:

_________________
gringo pussy cats - eef i see you i will pull your tail out by eets roots!


Zuletzt bearbeitet von Motzi am Do 23.02.06 14:37, insgesamt 1-mal bearbeitet
fidionael Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 232

Win XP SP2, Ubuntu 6.06
Delphi 7 PE, Delphi 3 Prof
BeitragVerfasst: Do 23.02.06 14:37 
Titel: Danke
Vielen lieben Dank die Zeile mit

Statistik:=TStatistik.Create war der springende Punkt ^^
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Do 23.02.06 14:42 
Fein - darüber bin ich nämlich auch schon oft gestolpert.

@Motzi: Das kann daran liegen, dass ich mir letzte Woche einen Fluxkompensator gekauft habe. Jetzt werden meine Posts immer 5 Minuten vor dem Schreiben gesendet :mrgreen:

_________________
We are, we were and will not be.
Einsamer Schütze
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 37

XP Prof
Delphi 2007 professional
BeitragVerfasst: Di 14.11.06 20:36 
Hat mir auch geholfen, vielen Dank, ich begehe nun keine weitere access violation und fühle mich gewaltloser :wink:

_________________
'Gott ist auf der Seite der Vernunft.' H. Mann