Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi AV bei Umwandlung der Klasse (https://www.delphipraxis.net/35611-av-bei-umwandlung-der-klasse.html)

Die Muhkuh 9. Dez 2004 17:54


AV bei Umwandlung der Klasse
 
Hi,


ich steh grad aufem Schlauch. Folgender Code:

Delphi-Quellcode:
type
  TEintrag = class
    Fenster: String;
    Titel: String;
    Caption: String;
  end;

[...]

procedure Tfmain.Button1Click(Sender: TObject);
var
  Eintrag: TEintrag;
  Fenster: String;
  Titel: String;
begin
  Eintrag := TEintrag.Create;
  Eintrag := TEintrag(Listbox1.Items[ListBox1.ItemIndex]);
  ShowWindow(FindWindow(PChar(Eintrag.Fenster), PChar(Eintrag.Titel)), SW_HIDE); //<---
  ListBox1.Items.Strings[Index] := ListBox1.Items.Strings[Index] + '|*';
  Button1.Enabled := false;
  Button2.Enabled := true;
end;


// Hinzufügen
procedure Tfadd.Button2Click(Sender: TObject);
begin
  with TEintrag.Create do
  begin
    Titel := lTitel.Caption;
    Fenster := lFenster.Caption;
    Caption := lTitel.Caption;
    fmain.ListBox1.AddItem(Caption, Self);
  end;
end;
Beim Pfeil gibts ne AV. Warum?

mirage228 9. Dez 2004 17:59

Re: AV bei Umwandlung der Klasse
 
Hi,

Delphi-Quellcode:
Eintrag := TEintrag(Listbox1.Items[ListBox1.ItemIndex]);
seit wann lässt sich ein String auf eine Klasse casten? :shock:

mfG
mirage228

Die Muhkuh 9. Dez 2004 18:01

Re: AV bei Umwandlung der Klasse
 
Wie sollte ich es sonst machen? :oops:

mirage228 9. Dez 2004 18:04

Re: AV bei Umwandlung der Klasse
 
Hi,

wie wäres mit

Delphi-Quellcode:
Items.Objects[]
:roll:

Und nächster Fehler:
Delphi-Quellcode:
  with TEintrag.Create do
  begin
    Titel := lTitel.Caption;
    Fenster := lFenster.Caption;
    Caption := lTitel.Caption;
    fmain.ListBox1.AddItem(Caption, Self);
  end;
Self = Form1 und nicht die Instanz von TEintrag. Also musst du wohl mit ner Variable arbeiten.

mfG
mirage228

jim_raynor 9. Dez 2004 18:05

Re: AV bei Umwandlung der Klasse
 
Zu dem erzeugst du mit Eintrag := TEintrag.Create; in Button1Click ein Speicherleck, weil du eine Zeile später Eintrag wieder überschreibst.

Die Muhkuh 9. Dez 2004 18:10

Re: AV bei Umwandlung der Klasse
 
Hi,


heut ist wohl nicht mein Tag :(.

Aber Thx. :) Jetzt gehts.


*MFG*

Mani :hi:


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