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 Problem beim Erstellen einer klasse (https://www.delphipraxis.net/79558-problem-beim-erstellen-einer-klasse.html)

Axxus 24. Okt 2006 19:20


Problem beim Erstellen einer klasse
 
Hi Delphianer

Ich fang grad an ma was mir Klassen zu amchen aber ich komm net weiter seht euch ma den Quelltext an und sagt mir was da falsch ist

Delphi-Quellcode:
unit Unit1;

interface

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

type
  TKonsole_Form = class(TForm)
    Konsole_Edit: TEdit;
    Konsole_Memo: TMemo;
    Konsole_Button: TButton;
    procedure Konsole_ButtonClick(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

type
  TFormelsammlung = class(TObject)
    {Variablen}
     Var_Konsole_Layout_Form_color_Background,
     Var_Konsole_Layout_Form_color_Background_Change1,
     Var_Konsole_Layout_Form_color_Background_Change2,
     Var_Konsole_Layout_Form_color_Background_Change3 : String;

    {Methoden}
    Constructor Create;
    procedure Set_Konsole_Layout_Form_color_Background (Txt: String);
    function Get_Konsole_Layout_Form_color_Background : String;
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Konsole_Form: TKonsole_Form;
  Formelsammlung: TFormelsammlung;
  Eingabe : String;

implementation

{$R *.dfm}

Constructor TFormelsammlung.Create;
begin
  inherited Create;
end;

procedure TFormelsammlung.Set_Konsole_Layout_Form_color_Background(Txt: string);
begin
  Var_Konsole_Layout_Form_color_Background := (Txt);
  Var_Konsole_Layout_Form_color_Background_Change1 := copy(Var_Konsole_Layout_Form_color_Background, 1, 2);
  Var_Konsole_Layout_Form_color_Background_Change2 := copy(Var_Konsole_Layout_Form_color_Background, 3, 4);
  Var_Konsole_Layout_Form_color_Background_Change3 := copy(Var_Konsole_Layout_Form_color_Background, 5, 6);
  Var_Konsole_Layout_Form_color_Background := '$' + Var_Konsole_Layout_Form_color_Background_Change3 + Var_Konsole_Layout_Form_color_Background_Change2 + Var_Konsole_Layout_Form_color_Background_Change1;
end;

function TFormelsammlung.Get_Konsole_Layout_Form_color_Background;
begin
  Konsole_Form.Color := (stringtocolor (Var_Konsole_Layout_Form_color_Background));
end;

procedure TKonsole_Form.Konsole_ButtonClick(Sender: TObject);
begin
Formelsammlung.Set_Konsole_Layout_Form_color_Background (Konsole_Edit.Text);
Formelsammlung.Get_Konsole_Layout_Form_color_Background;
end;

end.
So Starten geht aber wenn ich dann den RGB wehrt ins edit eintrage und auf den Button drücke dann Kommt folgende Fehlermldung:

Im Project1.exe ist ein exeption der Klasse EAccesViolation mit der Meldung 'Zugriffsverletzung bei Addresse 004046C8 in Modul 'Project1.exe'. Schreiben von Addresse 00000004' aufgetreten

weiß einer was da los is und wie ich das beheben kann????

Danke im Vorraus

Axxus

inherited 24. Okt 2006 19:24

Re: Problem beim Erstellen einer klasse
 
Ja. Du musst zunächst eine Instant von TFormelsammlung im OnCreate der Form erzeugen
Delphi-Quellcode:
var
  Formelsammlung: TFormelsammlung;

...

procedure TKonsolen_Form.FormCreate(Sender: TObject);
begin
  Formelsammlung:= TFormelsammlung.Create;
end;

Axxus 24. Okt 2006 22:16

Re: Problem beim Erstellen einer klasse
 
jo das wars hab net gewusst das man(n) das noch ma exrta schreiben muss hab gedacht das würde der Constructor erledigen aber der muss anscheinend noch ma extra aufgerufen werden

Danke Leute

Axxus

Jürgen Thomas 25. Okt 2006 11:28

Re: Problem beim Erstellen einer klasse
 
Zur Erklärung:

1. Jeder Konstruktor muss aufgerufen werden, entweder automatisch durch Delphi (siehe den Quelltext der Projektdatei) oder manuell durch den Entwickler - siehe Nikolais Hinweis.

2. In aller Regel kann zur Laufzeit nicht mit einer Klasse (also dem Bauplan eines Werkzeugs) gearbeitet werden, sondern nur mit Objekten, die im Arbeitsspeicher tatsächlich vorhanden sind (also einem Werkzeug, das nach dem Bauplan erstellt worden ist). Bevor Du mit einem Objekt - d.h. einer Instanz einer Klasse - arbeitest, musst Du sicherstellen, dass es existiert.

3. Delphi trennt zwischen Deklaration und Implementation. An der Stelle
Delphi-Quellcode:
var Formelsammlung: TFormelsammlung;
gibst Du nur dem Compiler bekannt, dass Du mit einem solchen Objekt zu arbeiten gedenkst; aber sonst passiert hier noch nichts.

Ich hoffe, ich konnte zum Verständnis beitragen. Gruß Jürgen

KrasserChecker 25. Okt 2006 12:23

Re: Problem beim Erstellen einer klasse
 
Zitat:

Zitat von inherited
Ja. Du musst zunächst eine Instant von TFormelsammlung im OnCreate der Form erzeugen
Delphi-Quellcode:
var
  Formelsammlung: TFormelsammlung;

...

procedure TKonsolen_Form.FormCreate(Sender: TObject);
begin
  Formelsammlung:= TFormelsammlung.Create;
end;


Richtig, aber da der Entwickler bei selbst erzeugten Objekten auch die "Verantwortung" für deren belegten Speicher übernimmt, muß es an geeigneter Stelle auch wieder freigegeben werden:
Delphi-Quellcode:
procedure TKonsolen_Form.FormDestroy(Sender: TObject);
begin
  Formelsammlung.Free;
end;
Sonst kommt es u.U. zu Speicherlecks...

Axxus 25. Okt 2006 17:18

Re: Problem beim Erstellen einer klasse
 
Jetzt ma ne frage leute hab ich das oben mit procedure und function richtiig gemacht und kann mir vieleicht einer ma den unterschied verdeutlichen ich hab gelesen das ne function nen Wert zurückgeben muss aber in der procedure oben wird doch genau das gleiche gemacht kann mir das einer noch ma erklären????????

Axxus

mkinzler 25. Okt 2006 17:24

Re: Problem beim Erstellen einer klasse
 
Wie du schon geschrieben hast: Funktionen haben einen Rückgabewert und Prozeduren keinen.

Delphi-Quellcode:
function Fa(Integer): Boolean; <--Rückgabeparameter

b := Fa( 1);

procedure Pa( Integer);

Pa(1);

Axxus 25. Okt 2006 17:41

Re: Problem beim Erstellen einer klasse
 
bedeuted das das Fa so waas wie ne variable ist die einene wert annehmen kann??? und Pa nicht???

mkinzler 25. Okt 2006 17:47

Re: Problem beim Erstellen einer klasse
 
Fa nimmt keinen wert an, sondern gibt einen zurück, genauso wie eine mathematische Funktion.

y=f(x) = x*2;

x=1 -> y = 2*1 = 1
x=2 -> y = 2*2 = 4

Delphi-Quellcode:
function f( x: Extended): Extended;
begin
    result := x*2;
end;
...
x := 1;
y := f(x); //-> y = 1
x := 2;
y := f(x); //-> y = 4

Axxus 25. Okt 2006 17:56

Re: Problem beim Erstellen einer klasse
 
ich glaub ic h habs jetzt
f(x) ist also der Platzhalter für result := x*2 und mit wert zurückgeben heißt dann das der rechner für f(x) result := x*2 einsetzt dann wäre result := x*2 der zurückgegebene wert ist das richtig???

mkinzler 25. Okt 2006 17:58

Re: Problem beim Erstellen einer klasse
 
Jein. Es ist kein Platzhalter/Makro, sondern ein Unterprogramm, welches aufgerufen wird und einen Wert zurückliefert.

Axxus 25. Okt 2006 18:41

Re: Problem beim Erstellen einer klasse
 
JO heißt im KLartext

Sobald das Programm zu einer funktion kommt springt es dorthin berechnet was und das ergebniss gibt es dann an der stelle zurück *bling* *Kronleuchter angegangen*
ok danke
hab jetzt wieder ien problem
Es rtitt wieder ein Exeption auf

Delphi-Quellcode:
unit Matrix_Windows_Controler_Quelltext;

interface

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

type
  TKonsole_Form = class(TForm)
    Konsole_Edit: TEdit;
    Konsole_Memo: TMemo;
    Konsole_Button: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDesrtoy(Sender: TObject);
    procedure Konsole_ButtonClick(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

type
  TFormelsammlung = class(TObject)
    {Variablen}
     Var_Konsole_Layout_Form_color_Background,
     Var_Konsole_Layout_Form_color_Background_Change1,
     Var_Konsole_Layout_Form_color_Background_Change2,
     Var_Konsole_Layout_Form_color_Background_Change3,
     Var_Konsole_Layout_Memo_color_Background,
     Var_Konsole_Layout_Memo_color_Background_Change1,
     Var_Konsole_Layout_Memo_color_Background_Change2,
     Var_Konsole_Layout_Memo_color_Background_Change3 : String;
     Konsole_Memo: TMemo;

    {Methoden}
    Constructor Create;
    procedure Set_Konsole_Layout_Form_color_Background (Txt: String);
    function Get_Konsole_Layout_Form_color_Background : String;
    procedure Set_Konsole_Layout_Memo_color_Background (Txt: String);
    function Get_Konsole_Layout_Memo_color_Background : String;

  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Konsole_Form: TKonsole_Form;
  Formelsammlung: TFormelsammlung;
  Eingabe : String;

implementation

{$R *.dfm}

Constructor TFormelsammlung.Create;
begin
  inherited Create;
end;

procedure TFormelsammlung.Set_Konsole_Layout_Form_color_Background(Txt: string);
begin
  Var_Konsole_Layout_Form_color_Background := (Txt);
  Var_Konsole_Layout_Form_color_Background_Change1 := copy(Var_Konsole_Layout_Form_color_Background, 1, 2);
  Var_Konsole_Layout_Form_color_Background_Change2 := copy(Var_Konsole_Layout_Form_color_Background, 3, 2);
  Var_Konsole_Layout_Form_color_Background_Change3 := copy(Var_Konsole_Layout_Form_color_Background, 5, 2);
  Var_Konsole_Layout_Form_color_Background := '$' + Var_Konsole_Layout_Form_color_Background_Change3 + Var_Konsole_Layout_Form_color_Background_Change2 + Var_Konsole_Layout_Form_color_Background_Change1;
end;

function TFormelsammlung.Get_Konsole_Layout_Form_color_Background;
begin
  Konsole_Form.Color := (stringtocolor (Var_Konsole_Layout_Form_color_Background));
end;

procedure TFormelsammlung.Set_Konsole_Layout_Memo_color_Background(Txt: string);
begin
  Var_Konsole_Layout_Memo_color_Background := (Txt);
  Var_Konsole_Layout_Memo_color_Background_Change1 := copy(Var_Konsole_Layout_Memo_color_Background, 1, 2);
  Var_Konsole_Layout_Memo_color_Background_Change2 := copy(Var_Konsole_Layout_Memo_color_Background, 3, 2);
  Var_Konsole_Layout_Memo_color_Background_Change3 := copy(Var_Konsole_Layout_Memo_color_Background, 5, 2);
  Var_Konsole_Layout_Memo_color_Background := '$' + Var_Konsole_Layout_Memo_color_Background_Change3 + Var_Konsole_Layout_Memo_color_Background_Change2 + Var_Konsole_Layout_Memo_color_Background_Change1;

end;

function TFormelsammlung.Get_Konsole_Layout_Memo_color_Background;
begin
  Konsole_Memo.color := (stringtocolor (Var_Konsole_Layout_Memo_color_Background));
end;



procedure TKonsole_Form.FormCreate(Sender: TObject);
begin
Formelsammlung := TFormelsammlung.Create;
end;

procedure TKonsole_Form.Konsole_ButtonClick(Sender: TObject);
begin
Formelsammlung.Set_Konsole_Layout_Memo_color_Background (Konsole_Edit.Text);
Formelsammlung.Get_Konsole_Layout_Memo_color_Background;
end;


procedure TKonsole_Form.FormDesrtoy(Sender: TObject);
begin
Formelsammlung.Free;
end;



end.
Wisst ihr was los is wenn nötig geb ich auch noch die fehlermeldung an

mkinzler 25. Okt 2006 18:47

Re: Problem beim Erstellen einer klasse
 
Zitat:

Wisst ihr was los is wenn nötig geb ich auch noch die fehlermeldung an
und am Besten noch die Zeile, in der die Exception auftritt

Axxus 26. Okt 2006 11:38

Re: Problem beim Erstellen einer klasse
 
Sorry das es erst jetzt kommt

Fehlermeldung:

In Project Matrix_Windows_controler.exe ist ein exeption der Klasse EAccessViolation mit der Meldung
'zugriffsverletzung bei Addresse 00438124 in modul Matrix_Windows_controler.exe lesen von Adresse 00000070' Aufgetreten

Zeile gibt er net an

Wisst ihr worans liegt???

Axxus

Jürgen Thomas 26. Okt 2006 13:19

Re: Problem beim Erstellen einer klasse
 
Hallo,
dafür gibt es den Debugger: Haltepunkt (Breakpoint) auf "verdächtige" Zeilen setzen und schrittweise durchgehen - dann wird es an einer bestimmten Zeile knallen, und dort ist das Problem.

Wenn Du keine Idee hast, wo das Problem sitzen könnte, dann setze den Haltepunkt möglichst früh.

AccessViolation deutet grundsätzlich darauf hin, dass Du ein Objekt ansprechen willst, das noch nicht erzeugt wurde.

Gruß Jürgen

KrasserChecker 26. Okt 2006 13:25

Re: Problem beim Erstellen einer klasse
 
Vielleicht solltest Du einfach mal versuchen der Variablen (die im übrigen "Feld" genannt wird...) "Konsole_Memo" aus der Klasse "TFormelsammlung" einen Wert zuzuweisen.

Sonst greifst Du nämlich in der Methode "Get_Konsole_Layout_Form_color_Background" ins Leere.

Etwa so:
Delphi-Quellcode:
procedure TKonsolen_Form.FormCreate(Sender: TObject);
begin
  Formelsammlung:= TFormelsammlung.Create;
  Formelsammlung.Konsole_Memo := Konsole_Memo;
end;

Axxus 26. Okt 2006 14:21

Re: Problem beim Erstellen einer klasse
 
Wieder was gelernt danke

Axxus


Alle Zeitangaben in WEZ +1. Es ist jetzt 20:57 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