![]() |
Re: Wie Array mit Pointern auf Klassen-Instanzen füllen?
Delphi-Quellcode:
unit Unit1;
interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, PWLists, ComCtrls, CommCtrl; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private fEdits : array of array of TObject; public procedure CreateEdits(aiWidth, aiHeight : Integer); end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin CreateEdits(10, 10); end; procedure TForm1.CreateEdits(aiWidth, aiHeight: Integer); var i, j : Integer; begin LockWindowUpdate(Handle); // hat nix mit Array zu tun SetLength(fEdits, aiHeight); for i := 0 to aiHeight - 1 do begin SetLength(fEdits[i], aiWidth); for j := 0 to aiWidth - 1 do begin fEdits[i, j] := TEdit.Create(Self); with fEdits[i, j] as TEdit do begin Parent := Self; SetBounds(34 * j + 16, 25 * i + 16, 30, 21); Text := Format('%d, %d', [i, j]); end; end; end; LockWindowUpdate(0); end; end. |
Re: Wie Array mit Pointern auf Klassen-Instanzen füllen?
Hier mal meins.
Die Klassen:
Delphi-Quellcode:
Und hier die Klasse TSand:
TWorld = class
private FWorldHeight: Integer; FWorldWidth: Integer; FBufferArray: array of array of TCreature; FWorldArray: array of array of TCreature; public constructor Create(WorldHeight, WorldWidth: Integer); destructor Destroy; override; end; TCreature = class private FName: string; public end; TGround = class(TCreature) private public end; TSand = class(TGround) private public constructor Create; destructor Destroy; end;
Delphi-Quellcode:
Sowie die Klasse TWorld:
constructor TSand.Create;
begin inherited Create; FName := 'Sand'; end;
Delphi-Quellcode:
constructor TWorld.Create(WorldHeight, WorldWidth: Integer);
var i: Integer; j: Integer; ArrayLength: Integer; begin FWorldHeight := WorldHeight; FWorldWidth := WorldWidth; ArrayLength := WorldHeight * WorldWidth; SetLength(FBufferArray, ArrayLength); SetLength(FWorldArray, ArrayLength); for i := 0 to FWorldHeight - 1 do for j := 0 to FWorldWidth - 1 do begin FBufferArray[i][j] := TGround.Create; end; end; |
Re: Wie Array mit Pointern auf Klassen-Instanzen füllen?
Und hier meine korrekturen
Delphi-Quellcode:
Versuch es mal so
constructor TWorld.Create(WorldHeight, WorldWidth: Integer);
var i: Integer; j: Integer; ArrayLength: Integer; begin FWorldHeight := WorldHeight; FWorldWidth := WorldWidth; SetLength(FBufferArray, WorldHeight, WorldWidth); SetLength(FWorldArray, WorldHeight, WorldWidth); for i := 0 to FWorldHeight - 1 do for j := 0 to FWorldWidth - 1 do begin FBufferArray[i][j] := TGround.Create; end; end; |
Re: Wie Array mit Pointern auf Klassen-Instanzen füllen?
Zitat:
Ich danke dir! :drunken: |
Re: Wie Array mit Pointern auf Klassen-Instanzen füllen?
Letzt Frage (wirklich! :wink: ):
Wie kann ich jetzt auf Variablen im Array zugreifen, ich meine:
Delphi-Quellcode:
Wo liegt der Fehler?
TCreature = class
private FName: string; public end; constructor TSand.Create; begin inherited Create; FName := 'Sand'; end; // --------------------------------- for i := 0 to FBlockNumX - 1 do for j := 0 to FBlockNumY - 1 do begin FBufferArray[i][j] := TSand.Create; FWorldArray[i][j] := TSand.Create; end; // --------------------------------- // Undeclared indetifier! if FWorldArray[i][j].FName := 'Sand' then ...; |
Re: Wie Array mit Pointern auf Klassen-Instanzen füllen?
Du musst noch das Private Element FName veröffentlichen.
Delphi-Quellcode:
TCreature = class
private FName: string; public property Name : String read fName; end; |
Re: Wie Array mit Pointern auf Klassen-Instanzen füllen?
Zitat:
|
Re: Wie Array mit Pointern auf Klassen-Instanzen füllen?
Klappt soweit, aber ich würde gerne eine Funktion schreiben, die den Namen zurückgibt. Dazu ein Frage: Wie kann ich die Instanz bei FWorldArray[x][y] per als Zeiger übergeben?
Habe sowas unter Object Pascal noch nie gemacht. Muss ich da einen Pointer übergeben oder einen Pointer auf TCreature? (d.h. übergebe ich "PCreature: Pointer" oder "PCreature: ^TCreature"?). |
Re: Wie Array mit Pointern auf Klassen-Instanzen füllen?
Delphi-Quellcode:
function GetName(fCreatur : TCreatur) : String;
begin Result := 'Name: ' + fCreatur.Name; end; |
Re: Wie Array mit Pointern auf Klassen-Instanzen füllen?
Ich dachte mir das so:
Delphi-Quellcode:
Naja, vielleicht hänge ich bei dieser Geschichte auch zu sehr an C++ ... :wink:
function TCreature.GetName(PCreatur : TCreatur): string;
begin Result := PCreatur.FName; end; [...] if MyCreature.GetName(^FWorldArray[i][j]) = 'Sand' then ... Edit: Wieso kann ich keine Stile im Delphi-Code nutzen!? |
Alle Zeitangaben in WEZ +1. Es ist jetzt 12:01 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz