Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Const expression in VBScript

Well, I try to understand limitations in Const expressions in VBScript. I was not able to use anything except literals. What the docs say is:

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

So, if "that includes all arithmetic or logical operators" then logically I expect I can do something like this:

Const X = (1 + 2)

But that brings the error "Expected literal constant". I found an interesting answer here that allows one to cheat, at some level, so the above can be done with:

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

But my question is about standard constant declaration. If by chance the docs said something like "expression could be ONLY literal", then I would never ask.
So what Else I can use (besides literal)?

like image 272
seeker Avatar asked Mar 20 '13 01:03

seeker


People also ask

What does const mean in VBScript?

Advertisements. Constant is a named memory location used to hold a value that CANNOT be changed during the script execution. If a user tries to change a Constant Value, the Script execution ends up with an error. Constants are declared the same way the variables are declared.

How do you declare a constant in VBScript?

Const MyStr = "Hello", MyNumber = 3.4567 ' Declare multiple constants on same line. Constants can make your scripts self-documenting and easy to modify. Unlike variables, constants cannot be inadvertently changed while your script is running.

What are constants in VB?

A constant is a meaningful name that takes the place of a number or string that does not change. Constants store values that, as the name implies, remain constant throughout the execution of an application.

What is a const statement?

Constants are block-scoped, much like variables declared using the let keyword. The value of a constant can't be changed through reassignment (i.e. by using the assignment operator), and it can't be redeclared (i.e. through a variable declaration).


1 Answers

Script56.chm says the following in the Remarks section:

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.

The bit in italics above makes a nonsense of the "or any combination that includes all arithmetic or logical operators except Is" claim.

like image 91
bugmagnet Avatar answered Sep 19 '22 14:09

bugmagnet