2012-05-26 54 views
0

previous question索引屬性的聲明,當我嘗試編譯此我得到這一行的錯誤不兼容的類型:E2008不兼容的類型在德爾福

Property player[i : integer] : TStringList read p; 

我不確定爲什麼?這是全班同學:

unit Battle; 

interface 

uses 
    SysUtils,Dialogs,Classes,inifiles, StdCtrls; 
type 

    TPlayers = class 
    Private 
    p : array[1..20] of TStringList; 
    FPlaceUnit: Boolean; 
    FTeamCount: Integer; 
    Public 
    Property player[i : integer] : TStringList read p; 
    property PlaceUnit : Boolean read FPlaceUnit write FPlaceUnit; 
    procedure AddPlayer (PlayerNo : integer; player : String); 
    property TeamCount : Integer read FTeamCount write FTeamCount; 

    constructor Create; virtual; 
    End; 


{Host class} 
    THostPlayers = Class(TPlayers) 
    Private 
    FIsHost: string; 
    Public 
    constructor Create; override; 
    property IsHost : string read FIsHost write FIsHost; 
End; 


{Guest Class} 
    TGuestPlayers = Class(TPlayers) 
    Private 
    FIsGuest: string; 
    Public 
    constructor Create; override; 
    property IsGuest : string read FIsGuest write FIsGuest; 
    End; 

implementation 

uses 
main; 
{constructor} 
constructor TPlayers.Create; 
begin 
    p := TStringList.Create; 
end; 
constructor THostPlayers.Create; 
begin 
    inherited; // Calls TPlayers.Create 
    IsHost := 'No'; 
    PlaceUnit := true; 
    TeamCount :=0; 
end; 
constructor TGuestPlayers.Create; 
begin 
    inherited; // Calls TPlayers.Create 
    IsGuest := 'No'; 
    PlaceUnit := true; 
    TeamCount := 0; 
end; 

{ADD Player} 
procedure TPlayers.AddPlayer(PlayerNo : integer; player : String); 
    var 
     CharINI : TIniFile; 
    begin 
     CharINI := Tinifile.Create(thisdir+'\char\charstats.ini'); 
     CharINI.ReadSectionValues(player,player[PlayerNo]); 
     CharINI.Free; 
    end; 
end. 

回答

2

首先,屬性「返回」一個的TStringList和現場p是TStringList中的數組所以這就是爲什麼你得到的不兼容類型的錯誤。

你會認爲:

// This is not working 
Property player[i : integer] : TStringList read p[i]; // Not supported... 

修復了這個。但你不能直接訪問數組元素,所以你需要一個getter函數:

TPlayers = class 
private 
    function GetPlayer(i: integer): TStringList; 

public 
    Property player[i : integer] : TStringList read GetPlayer 
end; 



function TPlayers.GetPlayer(i: integer): TStringList; 
begin 
    Result := p[i]; 
end; 
+0

ok我試過這個,但現在我得到錯誤,預計錯誤;但得到:後第二:在「getplayer(我:整數):tstringlist;」 –

+0

ahh納米,它的功能,而不是過程..:D –

+1

這是一種重複。請不要將每天發生的每個語法錯誤都發布爲一個新問題Glen。 :-) –