【问题标题】:How to create a dynamic array of single as a property in a class如何创建单个的动态数组作为类中的属性
【发布时间】:2009-09-18 08:46:46
【问题描述】:

我目前正在创建一个类来读写数组 打开一个文件,关闭一个文件都很好。 此外,我可以将数组写入 bin 文件。 但是从类中返回一个数组是通往远方的桥梁。
到目前为止,有 2 个问题我无法解决

1) 在公共部分 函数 ReadArrFromFile : 单数组; ==> 需要标识符,但发现数组和不兼容的类型单一和动态数组

2) 在函数 Tbinfiles.ReadArrFromFile : array of single 的实现中, ==> 我总是得到 E2029 标识符,但找到了数组

对于 1),如果我在主程序中定义单个数组,则不会导致任何问题 2) ReadArrFromFile 在主程序上工作正常

我正在使用 codegear RAD delphi 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.

【问题讨论】:

  • 欢迎来到 Stack Overflow。下次,请尝试将您的代码示例缩减为仍然存在问题的最小代码段。没有人愿意阅读所有代码只是为了找到语法错​​误。

标签: delphi arrays class


【解决方案1】:

您不能将数组作为属性,但可以将数组属性:

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;

【讨论】:

    【解决方案2】:

    您可以使用 named 类型 - 尝试来自单元 TypesTSingleDynArray。 但是使用数组属性(参见The_Fox's answer)可能更合适。

    【讨论】:

      【解决方案3】:

      1)首先声明数组类型..

      type
        TpMyarr = array of single;
      

      ...而且你做不到:

      function ReadArrFromFile : TpMyarr;
      

      2)在写入动态数组之前先调用SetLength。

      3) 不需要使用“self”。在你的程序中!

      4)BlockRead/BlockWrite 改用 TFileStream delphi 类。

      【讨论】:

      • 我忘了告诉你可以将动态数组作为属性。您必须在公共部分声明:property MyArr: TpMyarr read ReadArrFromFile;
      猜你喜欢
      • 2020-07-09
      • 1970-01-01
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      • 2020-01-08
      • 2012-04-20
      • 2023-03-19
      相关资源
      最近更新 更多