加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 如何在类中创建单个动态数组作为属性

发布时间:2020-12-15 09:37:30 所属栏目:大数据 来源:网络整理
导读:我目前正在创建一个用于编写和读取数组的类 打开文件,关闭文件一切正常. 此外,我能够向bin文件写一个数组. 但是从类中返回一个数组是远远的桥梁. 到目前为止,还有两个我无法解决的问题 1)在公共部分 ????????函数ReadArrFromFile:单个数组; ???==预期的标识
我目前正在创建一个用于编写和读取数组的类
打开文件,关闭文件一切正常.
此外,我能够向bin文件写一个数组.
但是从类中返回一个数组是远远的桥梁.
到目前为止,还有两个我无法解决的问题

1)在公共部分
????????函数ReadArrFromFile:单个数组;
???==>预期的标识符,但找到的数组&不兼容的类型单个和动态数组

2)在使用函数Tbinfiles.ReadArrFromFile实现:单个数组,
???==>我总是得到E2029标识符,但ARRAY找到了

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

我正在使用代码集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,self.pRecsread);
End;

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

function Tbinfiles.ReadArrFromFile : array of single  ;
begin
    BlockRead(self.pFKA,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.

解决方法

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

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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读