如何修复命名和范围冲突?

时间:2012-01-01 18:29:10

标签: delphi scope naming conflict

错误:实际和正式var参数的类型必须相同

unit unAutoKeypress;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Memo2: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
procedure SimulateKeyDown(Key:byte);
begin
keybd_event(Key,0,0,0);
end;

procedure SimulateKeyUp(Key:byte);
begin
keybd_event(Key,0,KEYEVENTF_KEYUP,0);
end;

procedure doKeyPress(var KeyValue:byte);
begin
 SimulateKeyDown(KeyValue);
 SimulateKeyUp(KeyValue);
end;



procedure TForm1.Button1Click(Sender: TObject);
const test = 'merry Christmas!';
var m: byte;
begin
Memo2.SetFocus();
m:=$13;
doKeyPress(m); // THIS IS WHERE ERROR
end;

end.

函数doKeyPress(m)中总是出错;  一个简单的问题,为什么?

我知道类型有问题,但所有类型都相似,到处都是字节,很奇怪  对我来说,我不能运行一个程序。

2 个答案:

答案 0 :(得分:7)

TForm继承自TWinControl,在DoKeyPress中声明了一个名为TWinControl的方法,该方法位于ButtonClick内编译器的当前范围内事件。

答案 1 :(得分:5)

问题是doKeyPressTForm1(继承自TWinControl)的方法,因此当您在doKeyPress方法中编写TForm1编译器时想要使用TForm1.doKeyPress而不是本地函数。类范围比本地函数范围更近。

可能的解决方案包括:

  • 重命名本地函数以避免冲突。
  • 使用完全限定名称unAutoKeypress.doKeyPress

在我看来,前者是更好的解决方案。

相关问题