Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi SetLength: AV (https://www.delphipraxis.net/86637-setlength-av.html)

Neutral General 16. Feb 2007 16:27


SetLength: AV
 
Hi,

Das ganze kommt mir seltsam vor... Also das ganze sieht so aus:

Delphi-Quellcode:
TPlantation = class(TBuilding)
//...
end;

TPlantations = Array of TPlantation;

TBuild = class
  private
    FOwner: TPlayer;
    procedure AddPlantation(AName: String);
  public
    constructor Create(AOwner: TPlayer);
    property Owner: TPlayer read FOwner;
    procedure Plantation(AName: String);
    destructor Destroy; override;
  end;

TPlayer = class
private
  FBuild: TBuild;
  FPlantations: TPlantations;
  //..
public
  constructor Create;
  property Build: TBuild read FBuild;
  property Plantations[Index: Integer]: TPlantation read GetPlantation write SetPlantation;
  //...
end;

constructor TPlayer.Create;
begin
  inherited Create;
  FBuild := TBuild.Create(Self);
end;

procedure TBuild.Plantation(AName: String);
begin
  if FOwner.Money >= UNIT_COSTS[UD_PLANTATION] then
  begin
    AddPlantation(AName);
    dec(FOwner.FMoney,UNIT_COSTS[UD_PLANTATION]);
  end
  else MessageDlg('Sie haben nicht genug Geld!',mtInformation,[mbOK],0);
end;

procedure TBuild.AddPlantation(AName: String);
begin
    SetLength(FOwner.FPlantations,Length(FOwner.FPlantations)+1); // Es knallt
    with FOwner.FPlantations[High(FOwner.FPlantations)].Create(FOwner) do
      Name := AName;
end;
Aufgerufen wirds im Hauptprogramm so:

Delphi-Quellcode:
Player := TPlayer.Create;
Player.Build.Plantation('Test');
//..
Ich versteh das Problem nicht so ganz :|
Da dürfte doch eigentlich nix schief gehn oder?...

Gruß
Neutral General

Neutral General 16. Feb 2007 16:52

Re: SetLength: AV
 
*räusper*

Erm... Die Sache hat sich erledigt.. :oops:
Aus irgendeinem Grund hab ich an einer Stelle

Delphi-Quellcode:
Instanz.Create;
statt

Delphi-Quellcode:
Instanz := Klasse.Create;
geschrieben... :oops: :wall:

Gruß
Neutral General

Khabarakh 16. Feb 2007 17:47

Re: SetLength: AV
 
Wie war das mit Arrays und wiederholter sukzessiver Verlängerung :zwinker: ?

Neutral General 16. Feb 2007 17:52

Re: SetLength: AV
 
:mrgreen:
Ach jooo^^ Ich hab immerhin ne Variable gespart *g*

Was für Nachteile hat das jetzt eigentlich außer das es länger dauert?

Khabarakh 16. Feb 2007 18:06

Re: SetLength: AV
 
Es verbraucht mehr Speicher :mrgreen: .
Für wirkliche Vorteile halte ich erweiterte Methoden (Insert, Remove), automatische Vergrößerung und OwnsObjects.
Zitat:

Zitat von Neutral General
Ich hab immerhin ne Variable gespart *g*

:gruebel: ?

Neutral General 16. Feb 2007 18:09

Re: SetLength: AV
 
Ehm vergiss es :mrgreen:

Thx für die Tipps^^


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