Thema: Delphi SetLength: AV

Einzelnen Beitrag anzeigen

Benutzerbild von Neutral General
Neutral General

Registriert seit: 16. Jan 2004
Ort: Bendorf
5.219 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#1

SetLength: AV

  Alt 16. Feb 2007, 16:27
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
Michael
"Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination,
but because their imagination reveals worlds that others cannot see."
  Mit Zitat antworten Zitat