Dim - VBScript


Dim

Overview

The Dim command is used to declare and optionally initialize variables in VB Script. It is a fundamental command for managing data and is essential for organizing and structuring code.

Syntax

Dim varName [As type] [= value]

Options/Flags

  • As type: Specifies the data type of the variable.
  • = value: Initializes the variable to the specified value.

Examples

  • Declare an integer variable named age:
Dim age
  • Declare a string variable named name and initialize it to “John Doe”:
Dim name As String = "John Doe"

Common Issues

  • Variable not declared: Failing to declare a variable before using it will result in a runtime error.
  • Invalid data type: Specifying an incorrect data type can lead to unexpected behavior or errors.

Integration

  • With other commands: Dim can be used in combination with commands like If and For to control program flow and manipulate data.
  • In scripts: Dim is used in VB Script scripts to define variables and store data for processing.

Related Commands

  • Option Explicit: Enforces explicit variable declaration in a script.
  • ReDim: Resizes an array variable.