How to create a dynamic array of solitary as properties in a class

I am currently creating a class for writing and reading arrays. Opening a file, closing a file works well. Alternatively, I can write an array to a bin file. But returning an array from a class is a bridge away. So far, there are 2 problems that I can’t work with.

1) in the public section, the ReadArrFromFile function: an array of single ones; ==> expected identifier, but found array and incompatible types of single and dynamic array

2) In the implementation with the function Tbinfiles.ReadArrFromFile: an array of single , ==> I always get the identifier E2029, but ARRAY found

For 1) if I define a single array in the main program, this does not cause any problems 2) the same for ReadArrFromFile works fine in the main program

I work with codegear RAD delphi 2007 and 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. 
+4
source share
3 answers

You cannot have an array as a property, but you can have array properties:

 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; 
+5
source

You can use a type called - try TSingleDynArray with a Types unit. However, using array properties (see The_Fox's answer ) might be more appropriate.

+4
source

1) First declare the type of the array ..

 type TpMyarr = array of single; 

... and than yo can:

 function ReadArrFromFile : TpMyarr; 

2) Before writing in a dynamic array, first call SetLength.

3) There is no need to use the "I". in your program!

4) Instead of BlockRead / BlockWrite, use the TFileStream delphi class.

0
source

All Articles