在函数内调用外部变量?

发布于 2024-12-22 18:26:27 字数 741 浏览 0 评论 0原文

我想将 Button1Click 事件处理程序的代码包装到名为 Show() 的函数中,以便稍后重用它。

事件处理程序代码:

procedure TForm2.Button1Click(Sender: TObject);
begin
  email := Form1.ed_Email.Text;
  password := Form1.Ed_typedpass.Text;

  MD5 := GetMD5;
  MD5.Init;
  MD5.Update(TByteDynArray(RawByteString(password)), Length(password));

  password := LowerCase(MD5.AsString);

end

当我将下面的代码放入脚本中时,我收到未声明的变量错误(因为我正在调用脚本中未声明的变量),但事实并非如此。

所有变量在程序内都能正常工作,但在函数内却不能正常工作?

function Show();
begin
  email := Form1.ed_Email.Text;
  password := Form1.Ed_typedpass.Text;

  MD5 := GetMD5;
  MD5.Init;
  MD5.Update(TByteDynArray(RawByteString(password)), Length(password));

  password := LowerCase(MD5.AsString);
end;

I want to wrap the code of Button1Click event handler into a function named Show() in order to reuse it later.

The event handler code:

procedure TForm2.Button1Click(Sender: TObject);
begin
  email := Form1.ed_Email.Text;
  password := Form1.Ed_typedpass.Text;

  MD5 := GetMD5;
  MD5.Init;
  MD5.Update(TByteDynArray(RawByteString(password)), Length(password));

  password := LowerCase(MD5.AsString);

end

When i put the code below into my script, i get non declared variables error ( as i am calling variables which are not declared inside my script ) but the reality isn't.

All variables work correctly inside procedures but not inside functions ?

function Show();
begin
  email := Form1.ed_Email.Text;
  password := Form1.Ed_typedpass.Text;

  MD5 := GetMD5;
  MD5.Init;
  MD5.Update(TByteDynArray(RawByteString(password)), Length(password));

  password := LowerCase(MD5.AsString);
end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丢了幸福的猪 2024-12-29 18:26:27

看起来您正在尝试在整个代码中应该使用局部变量的地方使用全局变量或成员变量。不要那样做。这是一种不好的做法,会给你将来带来巨大的痛苦。在以错误的方式编写太多代码之前改掉这个坏习惯。

尽可能使用局部变量。

procedure Show;
var
  email: string;
  password: string;
  MD5: TMD5;
begin
  email := Form1.ed_Email.Text;
  password := Form1.Ed_typedpass.Text;

  MD5 := GetMD5;
  MD5.Init;
  MD5.Update(TByteDynArray(RawByteString(password)), Length(password));
  password := LowerCase(MD5.AsString);
end;

我必须猜测 MD5 的类型,但您可以用实际类型替换它。

如果您确实需要使用成员,则可以将它们作为参数传递给该方法,或者使该过程成为拥有这些成员的表单类的方法。

这些变量似乎是 TForm2 的成员,如果您确实需要它们成为 TForm2 的成员,那么您可能应该将 Show 设为 <代码>TForm2方法。也就是说,Show 是一个错误的名称选择,因为它已经是 TForm 的一个方法。


我还猜测了 password 的类型,但从 MD5.Update 中进行的类型转换来看,它可能需要是 AnsiString称呼。或者,也许 Length 应该是 ByteLength。换句话说,我怀疑 MD5.Update 行中有一个错误,一旦您编译代码,该错误就会暴露出来。

It would appear that you are trying to use global variables, or member variables, throughout your code where local variables should be used. Don't do that. It's an bad practice that will give you enormous amounts of pain in the future. Lose that bad habit before you write too much code the wrong way.

Always use local variables where it is possible to do so.

procedure Show;
var
  email: string;
  password: string;
  MD5: TMD5;
begin
  email := Form1.ed_Email.Text;
  password := Form1.Ed_typedpass.Text;

  MD5 := GetMD5;
  MD5.Init;
  MD5.Update(TByteDynArray(RawByteString(password)), Length(password));
  password := LowerCase(MD5.AsString);
end;

I had to guess at the type of MD5 but you can substitute this with the actual type.

If you do need to use members then either pass them to the method as a parameter, or make the procedure a method of the form class that owns the members.

It seems likely that these variables are members of TForm2 and if you really need them to be members of TForm2 then you probably should make Show a TForm2 method. That said, Show is a bad choice of name since it is already a method of TForm.


I also guessed at the type for password but perhaps it needs to be AnsiString judging from the type-casting that goes on in the MD5.Update call. Or, perhaps Length should be ByteLength. In other words, I suspect that you have a bug in the MD5.Update line which will reveal itself once you get the code to compile.

雾里花 2024-12-29 18:26:27

Delphi 中的函数 不能没有返回类型。
尝试将其变成程序
另外,您还没有提到函数减速的基类。
试试这个

Procedure TForm1.Show();//TForm2 based on your decelaration
begin

 email := Form1.ed_Email.Text;
 password := Form1.Ed_typedpass.Text;

 MD5 := GetMD5;
 MD5.Init;
 MD5.Update(TByteDynArray(RawByteString(password)), Length(password));
 password := LowerCase(MD5.AsString);
end;

Function in Delphi cannot be without a return type.
Try making it into a Procedure.
Also you have not mentioned the base class for the function deceleration.
Try this

Procedure TForm1.Show();//TForm2 based on your decelaration
begin

 email := Form1.ed_Email.Text;
 password := Form1.Ed_typedpass.Text;

 MD5 := GetMD5;
 MD5.Init;
 MD5.Update(TByteDynArray(RawByteString(password)), Length(password));
 password := LowerCase(MD5.AsString);
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文