【问题标题】:Invalid typecast in a procedure [closed]过程中的无效类型转换[关闭]
【发布时间】:2013-03-19 20:35:36
【问题描述】:

我的程序代码在编译时总是给我带来问题。该程序的想法只是创建一个将文本文件读入数组的过程。然后该按钮将在 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.

【问题讨论】:

  • 您是否有任何理由不使用 TStringList ——“Delphi”方式来做到这一点?
  • 尼克使用 TStringList 是对的。此外,您还没有向我们展示 ArrNames 的定义。知道这会有所帮助。
  • 不要告诉我们它应该是什么。向我们展示它是什么。复制并粘贴代码中的声明。
  • 看起来你发布了假代码。请永远不要这样做。
  • 好的。您已经粘贴了所有代码。好的。谢谢你。 “无效类型转换”仍然是问题吗?如果不是,则更改问题的标题。一个 11 年级的学生应该能够认识到“编译时的问题”并不是一个充分的描述。请明确点。如果您在编译时遇到问题,那么您需要那个问题是什么。引用错误消息并指出错误发生在哪一行代码。

标签: delphi


【解决方案1】:

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

  • ShowData 永远不会被调用

  • ShowData 是个坏名字,因为它不显示任何内容而只是从文件中读取数据,所以最好将其重命名为 ReadData p>

  • aNamesaSales 是过程ShowData / 方法TForm1.btnShowDataClick本地 变量,并且生命周期仅在此过程/方法内。您无法访问其他过程/方法的局部变量。

    解决方案:将它们定义为TForm1的私有字段


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

a lot more of naming conventions


If FileExists( 'data.txt' ) &lt;&gt; true

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

if not FileExists( 'data.txt' )

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


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

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.

【讨论】:

  • 并将不合逻辑的 `If FileExists('data.txt') true then` 更改为 if not FileExists('data.txt') then。与真实的双重否定和不必要的比较让我头疼。
  • 如果文件的行数超过十行会怎样?最好将整个“ReadData”过程移动到按钮事件处理程序中,直接从文件中加载丰富的编辑组件,而不需要那些有限的数组。
  • @No'amNewman 1) 文件内的数据必须被转换/解释才能输出。 2)这是一个针对中间人的练习,其中一个目标是数组处理
  • 虽然我回答了我也投票结束因为不是一个真正的问题。它也可能因为过于本地化而被关闭。
  • 您实际上弄清楚了代码应该如何工作并为他修复了它?鲁弗爵士向您致敬。
猜你喜欢
  • 2017-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多