在函数内调用外部变量?
我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您正在尝试在整个代码中应该使用局部变量的地方使用全局变量或成员变量。不要那样做。这是一种不好的做法,会给你将来带来巨大的痛苦。在以错误的方式编写太多代码之前改掉这个坏习惯。
尽可能使用局部变量。
我必须猜测
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.
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 ofTForm2
then you probably should makeShow
aTForm2
method. That said,Show
is a bad choice of name since it is already a method ofTForm
.I also guessed at the type for
password
but perhaps it needs to beAnsiString
judging from the type-casting that goes on in theMD5.Update
call. Or, perhapsLength
should beByteLength
. In other words, I suspect that you have a bug in theMD5.Update
line which will reveal itself once you get the code to compile.Delphi
中的函数
不能没有返回类型。尝试将其变成
程序
。另外,您还没有提到函数减速的基类。
试试这个
Function
inDelphi
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