Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Findcomponent löst Exception aus??? (https://www.delphipraxis.net/31778-findcomponent-loest-exception-aus.html)

alcaeus 13. Okt 2004 20:10

Re: Findcomponent löst Exception aus???
 
Hi Thorben86,

du sollst das Ergebnis von FindComponent abspeichern, und nicht den Namen des Ergebnisses :zwinker:
Als Beispiel:
Delphi-Quellcode:
var
  c: TComponent;
begin
  c := TComponent(Findcomponent('im_st'+ InttoStr(1) + 'nr1'+ InttoStr(Stapel[1].Anzahl)));
  c.Name := ...
end;
Greetz
alcaeus

Thorben86 13. Okt 2004 20:15

Re: Findcomponent löst Exception aus???
 
Aso, dabei kommt aber glaub ich die gleiche Exception raus(Es kommt auf jedenfall auc hne Zugriffsverletzung raus)

Thorben86 13. Okt 2004 20:32

Re: Findcomponent löst Exception aus???
 
Hmm, also bei einem durchgang klappt es irgendwie jetzt auf einmal. Bei dem 2. durchgang kommt jetz an der anderen stelle ne Exception :gruebel: .

Die Komplette FDunktion:
Delphi-Quellcode:
var Kompo:TComponent;
begin
Kompo:=TComponent(Findcomponent('im_st'+ InttoStr(Startstapel) + 'nr' + InttoStr(Stapel[Startstapel].Anzahl)) as TShape);
Showmessage(inttostr(2)); //Das wird noch angezeigt, dann kommt die Exct

(Findcomponent('im_st'+ InttoStr(Startstapel) + 'nr' + InttoStr(Stapel[Startstapel].Anzahl)) as TShape).Left:= Stapel[Zielstapel].left;

(Findcomponent('im_st'+ InttoStr(Startstapel) + 'nr' + InttoStr(Stapel[Startstapel].Anzahl)) as TShape).Top:= Stapel[Zielstapel].Top - (15 * (Anzahl+1)) ;

inc(Stapel[Startstapel].Anzahl,-1);
inc(Stapel[Zielstapel].Anzahl);
Kompo.Name:='im_st'+ InttoStr(Zielstapel) + 'nr' + InttoStr(Stapel[Zielstapel].Anzahl + 1);
end;

SirThornberry 13. Okt 2004 21:05

Re: Findcomponent löst Exception aus???
 
es wurde schon zig mal geschrieben das du das Ergebnis von TComponent erstmal auf eine Variable zuweisen sollst und diese dann auf ungleich "nil" überprüfen sollst. Wenn dir FindComponent bei
Delphi-Quellcode:
(Findcomponent('im_st'+ InttoStr(Startstapel) + 'nr' + InttoStr(Stapel[Startstapel].Anzahl)) as TShape)
zurückgibt weil die Komponente mit dem Namen nicht existiert ist klar das dann eine Exception kommt.
richtiger ist
Delphi-Quellcode:
var LShape: TShape;
begin
  LShape := Findcomponent('im_st'+ InttoStr(Startstapel) + 'nr' + InttoStr(Stapel[Startstapel].Anzahl));
  if LShape <> nil then
  begin
    LShape.Left:=Stapel[Zielstapel].left;
  end;
Warum schreibst du überhaupt
Delphi-Quellcode:
Kompo:=TComponent(Findcomponent('im_st'+ InttoStr(Startstapel) + 'nr' + InttoStr(Stapel[Startstapel].Anzahl)) as TShape);
Wenn du den Wert den du auf Kompo zugewiesen hast dann nicht nutzt? Zu dem Ist es überflüssig das ganze nach TShape zu casten wenn du es dann der Variablen "kompo" zuweist die vom Typ "TComponent" ist. Besser wäre die Variable "Kompo" als "TShape" zu declarieren und dann mit "Kompo" weiterzuarbeiten und nicht immer wieder "FindComponent" zu nutzen da dies auch ein Performanceeinbruch ist.

Thorben86 13. Okt 2004 21:46

Re: Findcomponent löst Exception aus???
 
Sry, dat ich nit früher antworten konnt, ming I-net war seit 10 aus :? .
Hatte das eben auch schon geänert, aber das bringt mir nichts, da es ne Exception gibt, wenn ich die Procedure zum 2. mal aufrufe(die Wird in ner Rekursiven Procedure mit aufgerufen) und auf die Variable zugreife(also z.b. Kompo.Left)

Edit: Beim 2. durchlauf wird Kompo irgendwie nil, nur wieso versteh ich nicht, da dürfte doch eigentlich kein unterschied sein.

Jetzt sieht der Text so aus (beim 2. Durchlauf ist Kompo=nil)
Delphi-Quellcode:
var Kompo:TShape;
begin
Kompo:=(Findcomponent('im_st'+ InttoStr(Startstapel) + 'nr' + InttoStr(Stapel[Startstapel].Anzahl)) as TShape);

if(Kompo=nil)then
Showmessage('nil');
Showmessage(inttostr(3));

Kompo.Left:= Stapel[Zielstapel].left - (Kompo.Width div 2);// - (Findcomponent('im_st'+ InttoStr(Startstapel) + 'nr' + InttoStr(Stapel[Startstapel].Anzahl)) as TShape).width;
Kompo.Top:= Stapel[Zielstapel].Top - (15 * (Anzahl+1)) ;
inc(Stapel[Startstapel].Anzahl,-1);
inc(Stapel[Zielstapel].Anzahl);
Kompo.Name:='im_st'+ InttoStr(Zielstapel) + 'nr' + InttoStr(Stapel[Zielstapel].Anzahl);
Showmessage(Kompo.name);

Robert_G 13. Okt 2004 22:30

Re: Findcomponent löst Exception aus???
 
Da gibt's IMHO nur 2 Sachen:
  1. Ich lasse mich jetzt mal nicht an FindComponent aus. :roll:Klick
  2. Was hindert dich daran dem Elementtypen deiner Collection (Stapel) noch eine Refferenz auf das dazugehörige TShape zu verpassen?
Punkt 2 macht Punkt 1 unnötig...

Thorben86 13. Okt 2004 22:41

Re: Findcomponent löst Exception aus???
 
Ahm, bei Punkt 2 wüsst ich nicht, was ich machen müsste, aber es klappt jetzt auf jedenfall, irgendwie hat der vorher die Anzahl falsch eingelesen.


Alle Zeitangaben in WEZ +1. Es ist jetzt 13:39 Uhr.
Seite 2 von 2     12   

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