-6

我的程序代码在编译时一直给我带来问题。该程序的想法只是创建一个将文本文件读入数组的过程。然后该按钮将在 Richedit 上显示它们。

这是原始代码:

unit Unit1;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, StdCtrls, ComCtrls;
type
 ArrNames = array [1..10] of string;
 ArrSales = array [1..10] of integer;
type
 TForm1 = class(TForm)
 btnShowData: TButton;
 redt1: TRichEdit;
 procedure btnShowDataClick(Sender: TObject);
  private

  public
{ Public declarations }
end;
Procedure Showdata;
 var
   Form1: TForm1;

implementation

{$R *.dfm}


 Procedure ShowData;
   var c2u : textfile;
     count : integer;
     aNames : arrNames;
     aSales : arrSales;
   Begin
    If FileExists('data.txt') <> true then
     begin
       Messagedlg('File does not exist', mtError, [mbOK], 0);
       Exit;
   end;
       Count :=0;
       AssignFile(c2u, 'data.txt');
       Reset(c2u);
       While Not EOF(c2u) do
         begin
            Inc(Count);
            readln (c2u, aNames[count]);
            readln (c2u, aSales[count]);
         end;
       Closefile(c2u);
    End;

  procedure TForm1.btnShowDataClick(Sender: TObject);
    var J : integer;
        aNames : arrNames;
        aSales : arrSales;
   begin
     redt1.lines.add(aNames[J] +#9 + 'R' +IntToStr(aSales[J]));
   end;

  end.
4

1 回答 1

7

现在有了你的真实代码,我将列出你的一些错误:

  • ShowData从未被调用

  • ShowData是一个坏名字,因为它不显示任何东西,只从文件中读取数据,所以最好将它重命名为ReadData

  • aNames并且aSales是过程/方法的局部变量,并且生命周期仅在此过程/方法内。您无法访问另一个过程/方法的局部变量。ShowDataTForm1.btnShowDataClick

    解决方案:将它们定义为TForm1


作为一个T的改进,您应该命名所有以(例如)开头的类型TMyType。这只是一个约定,但非常有帮助。

还有更多的命名约定


If FileExists( 'data.txt' ) <> true

没有错,但很糟糕,你应该把它写在你的脑海里“如果文件不存在,我会做一些不同的事情

if not FileExists( 'data.txt' )

更具可读性(并且不会让几个用户感到头疼;o))


这是包含所有改进和一些评论的完整单元。

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TArrNames = array [1 .. 10] of string;
  TArrSales = array [1 .. 10] of integer;

type
  TForm1 = class( TForm )
    btnShowData : TButton;
    redt1 : TRichEdit;
    procedure btnShowDataClick( Sender : TObject );
  private
    // private fields of TForm1
    aNames : TArrNames;
    aSales : TArrSales;

    procedure ReadData;  // now it is a private method of TForm1
  public
    { Public declarations }
  end;

  // procedure Showdata; -> renamed/moved to TForm1.ReadData

var
  Form1 : TForm1;

implementation

{$R *.dfm}

// procedure Showdata;
procedure TForm1.ReadData;
var
  c2u :   textfile;
  count : integer;
  // aNames : ArrNames;
  // aSales : ArrSales;
Begin
  // If FileExists( 'data.txt' ) <> true
  // better
  if not FileExists( 'data.txt' )
  then
    begin
      MessageDlg( 'File does not exist', mtError, [mbOK], 0 );
      Exit;
    end;
  count := 0;
  AssignFile( c2u, 'data.txt' );
  Reset( c2u );
  while not EOF( c2u ) do
    begin
      Inc( count );
      ReadLn( c2u, aNames[count] );
      ReadLn( c2u, aSales[count] );
    end;
  CloseFile( c2u );
End;

procedure TForm1.btnShowDataClick( Sender : TObject );
var
  J : Integer;
  // aNames : ArrNames;
  // aSales : ArrSales;
begin
  // first, read the data
  ReadData;
  // loop over each array item
  for J := 1 to 10 do
    redt1.Lines.Add( aNames[J] + #9 + 'R' + IntToStr( aSales[J] ) );
end;

end.
于 2013-03-19T22:16:33.417 回答