Autor Beitrag
WormHole
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 145



BeitragVerfasst: Do 29.06.06 11:24 
Hi,
bin gerade dabei ein bissel OOP zu lernen und da hab ich mal einfach ein bissel losgetippt nur jetzt kommt ein Fehler bei folgender Stelle:

ausblenden Delphi-Quelltext
1:
2:
  for i:= 1 to 3 do
    TShape(FindComponent('Car_'+IntToStr(i))).Brush.Color := clbtnface;


Die Shapes mit den Namen: Car_1,Car_2,Car_3 werden zur Laufzeit erstellt...


Gruß
crowley
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 406

Win XP, Win Vista, Mandriva, Ubuntu
Delphi 4-8, Delphi 2006, Delphi 2007
BeitragVerfasst: Do 29.06.06 11:31 
bist du sicher, dass die Shapes zu diesem Zeitpunkt schon existieren ?

wo erzeugst du denn wann die Shapes? und wann rufst du deinen Quelltext auf?
WormHole Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 145



BeitragVerfasst: Do 29.06.06 11:36 
Jo die Shapes existieren dann schon. Ich habe das auf zwei Buttons verteilt:

Button 1(löst die CreateVehicle Proc aus):
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
procedure TVehicle.CreateVehicle;
var j: Integer; mycar : TCar;
begin
  for j := 1 to 3 do
  begin
  mycar := TCar.Create;
    with mycar do begin
      parent:= Form1;
      name  := 'Car_'+inttostr(j);
      top   := 30+j*10;
      left  := 50+j*10;
      width := 50;
      height:= 50;
      shape := stcircle;
      brush.Color := clRed;
    end;
  end;
end;


Button 2(löst die andere Prozedur aus):
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure TVehicle.beschleunigen(beschleunigung, endgeschwindigkeit:Integer);
var i:Integer;
begin
  Form1.la_status.caption := 'Beschleunigt!';
  for i:= 1 to 3 do
    TShape(FindComponent('Car_'+IntToStr(i))).Brush.Color := clbtnface;
end;


Hab keine Ahnung warum^^
Kann das damit zusammen liegen, dass ich verschiedene Klassen habe?
crowley
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 406

Win XP, Win Vista, Mandriva, Ubuntu
Delphi 4-8, Delphi 2006, Delphi 2007
BeitragVerfasst: Do 29.06.06 11:39 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure TVehicle.beschleunigen(beschleunigung, endgeschwindigkeit:Integer);  
var i:Integer;  
begin  
  Form1.la_status.caption := 'Beschleunigt!';  
  for i:= 1 to 3 do  
    TCar(FindComponent('Car_'+IntToStr(i))).Brush.Color := clbtnface;  
end;


versuch es mal so ?

was kommt denn genau für eine fehlermeldung?
WormHole Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 145



BeitragVerfasst: Do 29.06.06 11:44 
Ne hatte ich auch schon probiert...

ausblenden Quelltext
1:
[...]raised exeption class EAccess Violation[...]					
crowley
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 406

Win XP, Win Vista, Mandriva, Ubuntu
Delphi 4-8, Delphi 2006, Delphi 2007
BeitragVerfasst: Do 29.06.06 11:48 
okay... aber was für eine Klasse ist denn dein TCar ?
Wovon hast du die denn abgeleitet ?
WormHole Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 145



BeitragVerfasst: Do 29.06.06 11:51 
Die ist abgeleitet von TVehicle!

Mein Vater meinte gerade, vielleicht finded FindComponent die Komponente einfach nicht und deswegen...?!? Wie kriegt man das denn raus?
crowley
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 406

Win XP, Win Vista, Mandriva, Ubuntu
Delphi 4-8, Delphi 2006, Delphi 2007
BeitragVerfasst: Do 29.06.06 11:53 
wenn findcomponent = nil ist, wurde es nicht gefunden...

okay... TCar ist von TVehicle abgeleitet... und TVehicle von TShape ?
azubi_20
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 593

WinXP SP2, Ubuntu 8.4
D7 Enterp., D2005 Prof., Java (Eclipse 3.4.0)
BeitragVerfasst: Do 29.06.06 11:54 
Und was ist TVehicle ?
Sorry aber das ist ja dann nicht vom Typ TShape, sondern vom Typ TCar. Also hat dein Vater recht : FindComponent findet die Komponenten nicht, weil es sie nicht gibt...
WormHole Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 145



BeitragVerfasst: Do 29.06.06 12:17 
Ja TVehicle ist vom Typ TShape.
Und wie kann ich das Prob beheben?
crowley
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 406

Win XP, Win Vista, Mandriva, Ubuntu
Delphi 4-8, Delphi 2006, Delphi 2007
BeitragVerfasst: Do 29.06.06 12:21 
hm... poste am besten mal alles, was du da hast ... bzw. was in diesem zusammenhang interessant sein könnte... denn aus dem, was ich hier bisher lesen durfte, konnte ich nicht wirklich schlau werden...

Definition von TVehicle und TCar... sowie wann und wo du deine Cars erzeugst...

ach jaaa... was mir gerade noch aufgefallen ist... beim dynamischen Erzeugen deiner Cars... gib den Objekten auch mal einen Namen... sowas wie 'Car_'+ IntToStr(i)

evt findet er die Komponenten auch nicht, weil er nicht auf ihren Namen zugreifen kann
WormHole Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 145



BeitragVerfasst: Do 29.06.06 12:26 
Ich geb den Autos doch Namen... ?!?
Ehm, die Procedur wo ich atm die Farbe mit verändert habe, soll natürlich die shapes bewegen... Nur ich hatte FindComponent in einem anderen Zusammenhang benutzt und einfach kopiert erstmal... Müsste aber halt trotzdem klappen.

Hier der Code
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:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    bt_create: TButton;
    La_Statusinfo: TLabel;
    la_status: TLabel;
    Button1: TButton;
    procedure bt_createClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
  end;

  TVehicle = class(TShape)
    constructor Create;
    procedure CreateVehicle;
    procedure beschleunigen(beschleunigung, endgeschwindigkeit:Integer);
  end;
  TCar = class(TVehicle)
  end;

var
  Form1: TForm1;
  MyVehicle : TVehicle;

implementation

{$R *.dfm}

// lässt das Fahrzeug beschleunigen
procedure TVehicle.beschleunigen(beschleunigung, endgeschwindigkeit:Integer);
var i:Integer;
begin
  Form1.la_status.caption := 'Beschleunigt!';    // setzt das Status Label um
  for i:= 1 to 3 do                              // lässt die Fahrzeuge fahren
    TShape(FindComponent('Car_'+IntToStr(i))).Brush.Color := clbtnface;
end;

// erstellt die Fahrzeuge
procedure TVehicle.CreateVehicle;
var j: Integer; mycar : TCar;
begin
  for j := 1 to 3 do                             // erstellt 3 Autos
  begin
  mycar := TCar.Create;
    with mycar do begin                          // setzt die eigenschaften
      parent:= Form1;
      name  := 'Car_'+inttostr(j);
      top   := 30+j*10;
      left  := 50+j*10;
      width := 50;
      height:= 50;
      shape := stcircle;
      brush.Color := clRed;
    end;
  end;
end;

constructor TVehicle.Create;                     // der const von TVehicle
begin
  inherited create(MyVehicle);
end;

procedure TForm1.bt_createClick(Sender: TObject);// der Button für die
var MyVehicle : TVehicle;                        // Herstellung der Fahrzeuge
begin
  MyVehicle.CreateVehicle;
  bt_Create.Enabled := false;
end;

procedure TForm1.Button1Click(Sender: TObject);  // ATM der Button für besch-
var MyVehicle : TVehicle;                        // leunigung!
begin
  MyVehicle.beschleunigen(2,10);
end;

end.


Ehm wenn da jetz totaler schwachsinn manchmal steht...^^ Bin halt noch Noob :P
WormHole Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 145



BeitragVerfasst: Do 29.06.06 12:54 
Hab den Fehler gefunden....
Ich hab einfach den Konstruktor weggemacht -_-
Irgendwie muss mir nochmal einer die Funktion von dem Dingen genau erklären und mir sagen wie man den einsetzt.
crowley
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 406

Win XP, Win Vista, Mandriva, Ubuntu
Delphi 4-8, Delphi 2006, Delphi 2007
BeitragVerfasst: Do 29.06.06 13:30 
Moment mal... du rufst eine Funktion von TVehicle auf um die drei TCar auf deinem Formular zu erzeugen ? Nene... lass das mal... CreateVehicle sollte schon eine Funktion vom Form sein ;)

Ein constructor dient dazu, Eigenschaften mit Defaultwerten zu befüllen, Speicher zu reservieren bzw. benötigten interna deiner Klasse zu initialisieren ;)

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:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
unit Unit1;  
 
interface  
 
uses  
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  
  Dialogs, StdCtrls, ExtCtrls;  
 
type  
  TForm1 = class(TForm)  
    bt_create: TButton;  
    La_Statusinfo: TLabel;  
    la_status: TLabel;  
    Button1: TButton;  
    procedure bt_createClick(Sender: TObject);  
    procedure Button1Click(Sender: TObject);  
  private  
    { Private declarations }  
    procedure CreateVehicle;  
  public  
  end;  

 
  TVehicle = class(TShape)  
    procedure beschleunigen(beschleunigung, endgeschwindigkeit:Integer);  
  end;  

  TCar = class(TVehicle)  
  end;  

 
var  
  Form1: TForm1;  
  MyVehicle : TVehicle;  

 
implementation  

 
{$R *.dfm}  

 
// lässt das Fahrzeug beschleunigen  
procedure TVehicle.beschleunigen(beschleunigung, endgeschwindigkeit:Integer);  
var i:Integer;  
begin  
  Form1.la_status.caption := 'Beschleunigt!';    // setzt das Status Label um  
  for i:= 1 to 3 do                              // lässt die Fahrzeuge fahren  
    TShape(FindComponent('Car_'+IntToStr(i))).Brush.Color := clbtnface;  
end;  

// erstellt die Fahrzeuge  
procedure TForm1.CreateVehicle;  
var j: Integer; mycar : TCar;  
begin  
  for j := 1 to 3 do begin                        // erstellt 3 Autos  
                                                  // Ein Shape braucht doch im Constructor einen AOwner, oder ?
                                                  // also wenn's hier Probleme gibt, dann muss das heissen:
    mycar := TCar.Create;                         // mycar := TCar.Create(Self); 

    with mycar do begin                           // setzt die eigenschaften  
      parent:= Form1;  
      name  := 'Car_'+inttostr(j);  
      top   := 30+j*10;  
      left  := 50+j*10;  
      width := 50;  
      height:= 50;  
      shape := stcircle;  
      brush.Color := clRed;  
    end;  
  end;  
end;  

procedure TForm1.bt_createClick(Sender: TObject);// der Button für die  
var MyVehicle : TVehicle;                        // Herstellung der Fahrzeuge  
begin  
  MyVehicle.CreateVehicle;  
  bt_Create.Enabled := false;  
end;  

 
procedure TForm1.Button1Click(Sender: TObject);  // ATM der Button für besch-  
var MyVehicle : TVehicle;                        // leunigung!  
begin  
  MyVehicle.beschleunigen(2,10);  
end;  
 
end.
WormHole Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 145



BeitragVerfasst: Do 29.06.06 14:19 
Neues Problem:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
 for i:= 0 to 2 do                               // lässt die Fahrzeuge fahren
  begin
    if (richtung[i]=0) then TVehicle(FindComponent('Car_'+IntToStr(i))).Left :=
    TVehicle(FindComponent('Car_'+IntToStr(i))).Left - momspeed;  // Hier Fehler!
    if (richtung[i]=2) then TVehicle(FindComponent('Car_'+IntToStr(i))).Left :=
    TVehicle(FindComponent('Car_'+IntToStr(i))).Left + momspeed;
   end;
end;


Hier macht der wieder den EAccessViolation Fehler... Also in der markierten Zeile!

(info: richtung ist ein Array, das halt die Richtung bestimmen soll)
crowley
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 406

Win XP, Win Vista, Mandriva, Ubuntu
Delphi 4-8, Delphi 2006, Delphi 2007
BeitragVerfasst: Do 29.06.06 14:35 
markiere dir doch mal die zeile mit F5 und schau nach, was dir die findcomponent zurückgibt...

was ist momspeed ?

markiere mal den bereich, den du auswerten möchtest und drücke dann Strg+F7 (zur Laufzeit)...

hattest du oben meine Korrektur gesehen?
WormHole Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 145



BeitragVerfasst: Do 29.06.06 14:46 
momentane geschwindigkeit ^^
Ich liebe Deutsch - Englisch gemixt :P

Also, ja die Veränderungen habe ich gesehen und auch eingebaut, also die eine Prozedur in Form1! Wie krieg ich denn raus was FindComponent für nen Wert hat?

ausblenden Quelltext
1:
(FindComponent('Car_'+IntToStr(i))): Inaccessible value					


Steht da wenn ich beim Debugger, das anwähle...


Zuletzt bearbeitet von WormHole am Do 29.06.06 14:58, insgesamt 1-mal bearbeitet
crowley
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 406

Win XP, Win Vista, Mandriva, Ubuntu
Delphi 4-8, Delphi 2006, Delphi 2007
BeitragVerfasst: Do 29.06.06 14:58 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
  for i:= 0 to 2 do begin  
    if (richtung[i]=0) then 
      if FindComponent('Car_'+IntToStr(i)) = nil then
        ShowMessage('Keine Komponente mit Namen ' + 'Car_'+IntToStr(i) + ' gefunden!')
      else
        TCar(FindComponent('Car_'+IntToStr(i))).Left :=  
        TCar(FindComponent('Car_'+IntToStr(i))).Left - momspeed;  
    {...}  
  end;



Ach jaaaa... ich dachte, du hast dir drei TCar gebastelt... warum castest du dann auf TVehicle ??

noch eine Anmerkung... versuch deinen Quelltext gleichmäßig auszurichten, erleichter die lesbarkeit ;)
WormHole Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 145



BeitragVerfasst: Do 29.06.06 15:04 
Hmm ja jetzt steht fest... Er findet die Sachen nicht!

Versteh ich aber nicht, weil er die ja schonmal gefunden hatte...
crowley
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 406

Win XP, Win Vista, Mandriva, Ubuntu
Delphi 4-8, Delphi 2006, Delphi 2007
BeitragVerfasst: Do 29.06.06 15:10 
ich glaube, ich habe den fehler gefunden *gg*

schau dir nochmal an, welche TCar du erzeugst...

for i:= 1 to 3 do...

und jetzt beim beschleunigen oder was auch immer:

for i:= 0 to 2 do...

*gg*