MsgBox“"与VBScript中的MsgBox() [英] MsgBox "" vs MsgBox() in VBScript

查看:166
本文介绍了MsgBox“"与VBScript中的MsgBox()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个VBScript,并使用Randomize和MsgBox等函数。我很好奇使用()和不使用它们的区别是什么。例如:



随机化 - 此行可用。

Randomize() - 此行也可以。

MsgBoxHello world! - This works。



MsgBox(Hello world!) - This也可以运行。



该脚本将运行在具有不同Windows版本(至少Windows XP)的多台计算机上。我想知道是否会在使用这些函数时遇到任何兼容性/语法问题。

解决方案

可调用一段代码例程)可以是一个Sub(称为副作用/
)或函数(称为它的返回值)或两者的混合。由于文档
用于MsgBox


在对话框中显示一条消息,等待用户单击
按钮,并返回一个值,指示用户点击了哪个按钮。



MsgBox(提示[,按钮] [,标题] [,帮助文件,上下文])


表明这个例程是第三类的。



VBScript的语法规则是简单:

使用参数list()调用(例程为a)函数



如果您想向用户显示一条消息,并且需要知道用户的
响应:

  Dim MyVar 
MyVar = MsgBox(Hello World!,65,MsgBox Example)
'MyVar包含1或2,取决于哪个按钮被点击。

调用a(例程为a)时不要使用参数list / strong>



如果您想向用户显示一条消息,并且对响应中的
不感兴趣:

  MsgBoxHello World!,65,MsgBox Example


$
$ b

使用()的参数列表的设计缺陷并强制按值
语义


 >> Sub S(n):n = n + 1:End Sub 
>> n = 1
>> S n
>> WScript.Echo n
>> S(n)
>> WScript.Echo n
>>
2
2



并不意味着 n,但是用n的值拷贝调用S。
程序员看到

 >> s =价值
>> MsgBox(s)

'works'在尝试时会带来惊喜:

 >> MsgBox(s,65,MsgBox Example)
>>
错误编号:1044
错误描述:调用子
时不能使用括号

编译器对Sub调用中的empty()方面的宽容性。

 <$ 
Sub Randomize(调用随机种子的副作用)可以调用
c $ c> Randomize()

虽然()既不意味着给我你的返回值也不是通过
通过价值获得的东西。在这里稍微严格一点就会迫使prgrammers去
知道在

 随机化n 



< pre $ Randomize(n)

允许参数在子调用中使用list():

lockquote
$ block $ b $ s $ value
调用MsgBox(s, 65,MsgBox Example)



这进一步鼓励程序员不加思索地使用()。 / p>

(基于你的意思是不能使用圆括号吗?)

I'm trying to write a VBScript and I'm using functions such as Randomize, and MsgBox. I'm curious as to what is the difference of using () and not using them. For example:

Randomize - This line works.

Randomize() - This line works also.

MsgBox "Hello world!" - This works.

MsgBox ("Hello world!") - This works as well.

The script will be running on multiple machines with different versions of Windows (at least Windows XP). I'm wondering if I would be getting any compatibility/syntax issues in using these functions.

解决方案

A callable piece of code (routine) can be a Sub (called for a side effect/what it does) or a Function (called for its return value) or a mixture of both. As the docs for MsgBox

Displays a message in a dialog box, waits for the user to click a button, and returns a value indicating which button the user clicked.

MsgBox(prompt[, buttons][, title][, helpfile, context])

indicate, this routine is of the third kind.

The syntactical rules of VBScript are simple:

Use parameter list () when calling a (routine as a) Function

If you want to display a message to the user and need to know the user's reponse:

Dim MyVar
MyVar = MsgBox ("Hello World!", 65, "MsgBox Example")
   ' MyVar contains either 1 or 2, depending on which button is clicked.

Don't use parameter list () when calling a (routine as a) Sub

If you want to display a message to the user and are not interested in the response:

MsgBox "Hello World!", 65, "MsgBox Example"

This beautiful simplicity is messed up by:

The design flaw of using () for parameter lists and to force call-by-value semantics

>> Sub S(n) : n = n + 1 : End Sub
>> n = 1
>> S n
>> WScript.Echo n
>> S (n)
>> WScript.Echo n
>>
2
2

S (n) does not mean "call S with n", but "call S with a copy of n's value". Programmers seeing that

>> s = "value"
>> MsgBox(s)

'works' are in for a suprise when they try:

>> MsgBox(s, 65, "MsgBox Example")
>>
Error Number:       1044
Error Description:  Cannot use parentheses when calling a Sub

The compiler's leniency with regard to empty () in a Sub call. The 'pure' Sub Randomize (called for the side effect of setting the random seed) can be called by

Randomize()

although the () can neither mean "give me your return value) nor "pass something by value". A bit more strictness here would force prgrammers to be aware of the difference in

Randomize n

and

Randomize (n)

The Call statement that allows parameter list () in Sub calls:

s = "value" Call MsgBox(s, 65, "MsgBox Example")

which further encourage programmers to use () without thinking.

(Based on What do you mean "cannot use parentheses?")

这篇关于MsgBox“&quot;与VBScript中的MsgBox()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆