首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

函数嵌套变量传值有关问题

2012-02-29 
函数嵌套变量传值问题这位高人,能解释一下这是什么原因,要怎么让 iType 的值传过去Delphi(Pascal) codeuni

函数嵌套变量传值问题
这位高人,能解释一下这是什么原因,要怎么让 iType 的值传过去

Delphi(Pascal) code
unit Unit1;interface[code=Delphi(Pascal)]

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

type
  Tf = function(i: integer): string;
  TForm1 = class(TForm)
  procedure FormCreate(Sender: TObject);
  private
  { Private declarations }
  public
  { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure test(f:Tf);
begin
  showmessage( f(1) );
end;

procedure TForm1.FormCreate(Sender: TObject);
var iType: integer;
  function f1(i: integer): string;
  begin
  result := intToStr(i + iType);
  end;

begin
  iType := 1;
  showmessage(f1(1));  

  test( @f1 ); //样调用 iType 值就没了 
end;

end.

[/code]

[解决办法]
procedure test(f:Tf);
begin
showmessage( f(1) );
end;
这是在test中调用了procedure TForm1.FormCreate(Sender: TObject);中的子过程f1,iType既不在f1、也不在test的栈框架中,当然寻址不到了。

[解决办法]
哦。。。。明白

热点排行