2009-09-18 17 views
4

我目前正在創建一個類來寫入和讀取數組 打開一個文件,關閉一個文件一切正常。 另外,我可以寫入一個bin文件的數組。 但是從類中返回一個數組是遠遠的一個橋樑。
到目前爲止,ther're 2個問題在那裏,我不能工作的公共部分 功能ReadArrFromFile圍繞如何創建一個單一的動態數組作爲一個屬性在一個類

1):陣列的單; ==>標識符預期但陣列發現&不兼容的類型的單和動態數組

2)在該實施方式與功能Tbinfiles.ReadArrFromFile:單陣列, ==>我總是E2029標識符預期但ARRAY發現

爲1),如果我在主程序中定義的單個陣列它不會引起同爲ReadArrFromFile任何問題 2)工精細的主程序

我用的CodeGear RAD德爾福工作2007 & Windows Vista。

unit UbinFiles; 

interface 

type 
    TBinFiles = Class 
     private 
     pFileName  : String;   // File name (FILENAME.bin) 
     pFileType  : string;   // File type (of ..) 
     pFileLoc   : string;   // FileLocation path 
     pMyarr   : array of single; // array to receive/provide results 
     pArrLen   : integer;   // To define arraylength 
     pFKA    : file;   // File Known As or the internal name 
     pRecsWritten  : integer;   // # of blocks written towards file 
     pRecsRead  : integer;   // # of blocks read from file 

     public 
     procedure SetFname(const Value: String); 
     procedure SetFtype(const Value: String); 
     procedure SetFLoc(const Value: String); 
     procedure SetArrLen(const Value: integer); 

     constructor Create;             overload; 
     constructor Create(Fname : String);         overload; 
     constructor Create(Fname : String ; Ftype : string);     overload; 
     constructor Create(Fname : String ; Ftype : string ; FLoc : String); overload ; 

     procedure OpenMyFile; 
     procedure CloseMyFile; 
     procedure Write2MyFile(Myarr : array of single); 
     procedure ReadFromMyFile; 
     function CheckBackSpace(MyPath : string) : string ; 
     procedure TSTreadAnArray(Myarr : array of single); 
//---first problem 
     function ReadArrFromFile : array of single; 

     published 

     property Fname : String read pFileName write SetFname; 
     property Ftype : String read pFileType write SetFtype; 
     property FLoc : String read pFileLoc write SetFLoc; 
     property ArrLen : integer read pArrLen write SetArrLen; 

end; 

implementation 

uses 
    Dialogs, SysUtils, StrUtils; // controls required for this class 
// 
//---Constructors----------------------------- 
// 
constructor TBinFiles.Create;    // void constructor 
begin 
    inherited; 
    self.pFileName  := 'MyBinary'; 
    self.pFileType  := ''; 
    self.pFileLoc  := 'C:\Users\'; 
    self.pRecsWritten := 0; 
    self.pRecsRead  := 0; 
end; 

constructor TBinFiles.Create(Fname: String); // contructor + Fname 
begin 
    self.pFileName  := Fname; 
    self.pFileType  := ''; 
    self.pFileLoc  := 'C:\Users\'; 
    self.pRecsWritten := 0; 
    self.pRecsRead  := 0; 
end; 

constructor TBinFiles.Create(Fname: String ; Ftype : string); // constructor etc.. 
begin 
    self.pFileName  := Fname; 
    self.pFileType  := Ftype; 
    self.pFileLoc  := 'C:\Users\'; 
    self.pRecsWritten := 0; 
    self.pRecsRead  := 0; 
end; 

constructor TBinFiles.Create(Fname: String ; Ftype : string ; FLoc : string); 
begin 
    self.pFileName  := Fname; 
    self.pFileType  := Ftype; 
    self.pFileLoc  := CheckBackSpace(FLoc); 
    self.pRecsWritten := 0; 
    self.pRecsRead  := 0; 
end; 
// 
//----setters--------------------------------------- 
// 
procedure TBinFiles.SetFname(const Value: String); // pFileName 
begin 
    pFileName   := Value; 
end; 

procedure TBinFiles.SetFtype(const Value: String); // pFileType 
begin 
    pFileType   := Value; 
end; 

procedure TBinFiles.SetFLoc(const Value: String); // pFileLoc 
begin 
    pFileLoc   := Value; 
end; 

procedure TBinFiles.SetArrLen(const Value: integer); 
begin 
    pArrLen   := Value; 
end; 


// 
//---general functions/procs---- 
// 
procedure Tbinfiles.OpenMyFile; 
begin 
    try 
    AssignFile(self.pFKA, self.pFileLoc + self.pFileName +'.bin'); 
    ReWrite(self.pFKA); 
    except 
    on E : Exception do 
     begin 
     ShowMessage(E.ClassName+' error raised, with message : '+E.Message); 
     end; 
    End; 
end; 

procedure Tbinfiles.CloseMyFile; 
begin 
    CloseFile(self.pFKA); 
End; 

procedure Tbinfiles.Write2MyFile(Myarr : array of single); 
begin 
    BlockWrite(self.pFKA, Myarr, 1,self.pRecsWritten); 
End; 

procedure Tbinfiles.ReadFromMyFile; 
begin 
    BlockRead(self.pFKA, self.pMyarr, 1,self.pRecsread); 
End; 

//------second problem----------------------------------------------<<<<<< doesn't work 

function Tbinfiles.ReadArrFromFile : array of single ; 
begin 
    BlockRead(self.pFKA, self.pMyarr, 1,self.pRecsread); 
End; 

function Tbinfiles.CheckBackSpace(MyPath : string) : string ; 
begin 
    if AnsiRightStr(MyPath, 1) = '\' 
    then Result := MyPath 
    else Result := MyPath + '\' 
    ; 
end; 

procedure Tbinfiles.TSTreadAnArray(Myarr : array of single); 
var i:integer; 
begin 
    for i := 0 to high(Myarr) do 
    begin 
     showmessage('Element ' + intToStr(i)+ floatToStr(MyArr[i])); 
    end; 
end; 

end. 
+2

歡迎來到Stack Overflow。下一次,請嘗試將您的代碼示例修剪成仍然存在問題的最小代碼。沒人願意爲了找到語法錯​​誤而閱讀*所有*代碼。 – 2009-09-18 13:17:15

回答

5

你不能有一個數組作爲屬性,但你可以有數組屬性:

TMyObject = class 
private 
    function GetSingleArray(aIndex: Integer): Single; 
    procedure SetSingleArray(aIndex: Integer; const Value: Single); 
    function GetSingleArrayCount: Integer; 
    procedure SetSingleArrayCount(const Value: Integer); 
    public 
    property SingleArray[aIndex: Integer]: Single read GetSingleArray write SetSingleArray; 
    //returns or sets the length of the single array 
    property SingleArrayCount: Integer read GetSingleArrayCount write SetSingleArrayCount; 
end; 
4

您可以使用一個名爲 - 嘗試TSingleDynArray從單元Types。 但是使用數組屬性(請參閱The_Fox's answer)可能更合適。

0

1)首先聲明數組類型..

type 
    TpMyarr = array of single; 

...比喲可以這樣做:

function ReadArrFromFile : TpMyarr; 

2)首先在DINAMIC陣列調用SetLength刻錄之前。

3)沒有必要使用'自我'。在你的程序中!

4)改爲BlockRead/BlockWrite使用TFileStream delphi類。

+0

我忘記告訴你可以做屬性的動態數組。 您必須在公開部分聲明: property MyArr:TpMyarr read ReadArrFromFile; – 2009-09-19 10:48:24

相關問題