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

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

问题描述

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

Randomize - 此行有效.

Randomize() - 此行也适用.

MsgBox "Hello world!" - 这有效.

MsgBox ("Hello world!") - 这也有效.

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

解决方案

一段可调用的代码(例程)可以是一个 Sub(调用一个副作用/它是什么do) 或函数(调用其返回值)或两者的混合.作为文档用于消息框

<块引用>

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

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

指出,这个例程是第三种.

VBScript 的语法规则很简单:

调用(例程作为)函数时使用参数列表()

如果要向用户显示消息,需要知道用户的回复:

Dim MyVarMyVar = MsgBox ("Hello World!", 65, "MsgBox 示例")' MyVar 包含 1 或 2,具体取决于单击的按钮.

调用(例程作为)子时不要使用参数列表()

如果您想向用户显示消息并且不感兴趣在回复中:

MsgBox "Hello World!", 65, "MsgBox 示例"

这个美丽的简单被搞砸了:

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

<代码>>>Sub S(n) : n = n + 1 : End Sub>>n = 1>>SN>>WScript.Echo n>>S(n)>>WScript.Echo n>>22

S(n) 不是用n 调用S",而是用n 值的副本调用S".程序员看到了

<代码>>>s =价值">>消息框

作品"在尝试时会大吃一惊:

<代码>>>MsgBox(s, 65, "MsgBox 示例")>>错误编号:1044错误描述:调用 Sub 时不能使用括号

编译器对 Sub 调用中的空 () 的宽大处理.'纯'Sub Randomize(调用设置随机种子的副作用)可以被调用

Randomize()

虽然 () 既不能表示给我你的返回值)也不能表示通过一些价值".这里更严格一点会迫使程序员请注意

随机化 n

随机化 (n)

允许在子调用中使用参数列表()的Call语句:

<块引用><块引用>

s = "值"调用 MsgBox(s, 65, "MsgBox Example")

这进一步鼓励程序员不假思索地使用 ().

(基于 不能使用括号"是什么意思?)

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 ""VS VBScript 中的 MsgBox()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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