2013-03-20 26 views
5

好吧,我試圖理解Const表達式中的限制VBScript。除了文字之外,我無法使用任何東西。什麼the docs say是:瞭解VBScript中的Const表達式

Literal or other constant, or any combination that includes all arithmetic or logical operators exceptIs.

所以,如果「包括所有的算術或邏輯運算符」,那麼在邏輯上我希望我可以做這樣的事情:

Const X = (1 + 2) 

但是,這帶來的錯誤「預期字面常量」。我發現一個有趣的答案here,允許一個作弊,在一定程度上,讓上面的功能也可以用:

Execute "Const X = " & (1 + 2) 

但我的問題是關於標準定聲明。如果偶然的文件說像「表達可能只是字面意思」,那麼我永遠不會問。
那麼還有什麼我可以使用(除了文字)?

+0

微軟的舊模糊不清。如果你做第一個沒有括號的話,會發生什麼? – bugmagnet 2013-03-20 01:22:25

+0

test.vbs(1,16)Microsoft VBScript編譯錯誤:預期文字常量 – bugmagnet 2013-03-20 01:25:31

+0

感謝您的快速回復。如果削減括號,沒有差異,相同的錯誤 – seeker 2013-03-20 01:26:17

回答

3

Script56.chm說,在備註部分執行以下操作:

Constants are public by default. Within procedures, constants are always private; their visibility can't be changed. Within a script, the default visibility of a script-level constant can be changed using the Private keyword.

To combine several constant declarations on the same line, separate each constant assignment with a comma. When constant declarations are combined in this way, the Public or Private keyword, if used, applies to all of them.

You can't use variables, user-defined functions, or intrinsic VBScript functions (such as Chr) in constant declarations. By definition, they can't be constants. You also can't create a constant from any expression that involves an operator, that is, only simple constants are allowed. Constants declared in a Sub or Function procedure are local to that procedure. A constant declared outside a procedure is defined throughout the script in which it is declared. You can use constants anywhere you can use an expression.

斜體位以上使的無義「包括所有的算術或除了邏輯運算符或任何組合爲」權利要求。

+0

呵呵,公然對立:)所以,你建議的是備註部分是正確的,對吧? – seeker 2013-03-20 05:01:49

+0

這是我的承諾,是的。也許這是兩個(或更多?)人寫了文檔,他們沒有很好地溝通,或者Const表示法會變得更強大,但最終還是沒有完成。無論哪種方式,我們都有矛盾,而且我對VBScript的經驗也支持備註閱讀。 – bugmagnet 2013-03-20 05:30:40

+0

我會依靠你的經驗,並接受這個答案,謝謝。 – seeker 2013-03-20 05:41:47