Search code examples
variablesvbscriptqtpinitializing

How to initialize multiple variables together?


I need to initialize several variables with the same value in VBScript. The only way I can find is for example x = 5 : y = 5 : z = 5. Is there a way similar to x = y = z = 5?


Solution

  • VBScript doesn't support multiple assignments. A statement

    x = y = z = 5
    

    would be evaluated like this (pseudocode using := as assignment operator and == as comparison operator to better illustrate what's happening):

    x := ((y == z) == 5)
    x := ((Empty == Empty) == 5)
    x := (True == 5)
    x := False
    

    As a result the variable x will be assigned the value False while the other variables (y and z) remain empty.

    Demonstration:

    >>> x = y = z = 5
    >>> WScript.Echo TypeName(x)
    Boolean
    >>> WScript.Echo "" & x
    False
    >>> WScript.Echo TypeName(y)
    Empty
    >>> WScript.Echo TypeName(z)
    Empty

    The statement

    x = 5 : y = 5 : z = 5
    

    isn't an actual multiple assignment. It's just a way of writing the 3 statements

    x = 5
    y = 5
    z = 5
    

    in a single line (the colon separates statements from each other in VBScript).