What are VBScript Constants?

1
57
Spread the love
  • 29
  • 23
  • 24
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    123
    Shares

VB Script Constants
VB Script Constants

Constants in VBScript:

A constant is such a variable or a meaningful name that takes the place of a number or string and it never changes during runtime. Constants have defined storage.In VBScript there two type of constants:

  1. User defined or named constants or custom constants
  2. Built in Constanats

Interestingly, we can not change the value of the constants from another script which is outside of our main script.

Named Constants or User defined constants or custom constants:

Named constants or user defined constants or custom constants are the constants defined using const keyword.Named constants have scope and can be of private or public type.(We have to use const keyword instead of Dim). The scope will follow variable scope qualification.

Note: Constant value can not be output of some function.As they have been designed during design time and run time it will be constant.

Creating Constants:

We create user-defined constants in VBScript using the Const statement. Using the Const statement, we can create a string or numeric constants with meaningful names and assign them literal values.Even though there is no hard and fast rule for User defined constant but a good practice i to declare as all uppercase. This will clearly differentiate a variable with a constant.

Advantages of Constants:

  • Constants are easier to understand and code.
  • Constants improve code clarity as the constant name carries the actual meaning of usage of the constant.
  • It removes the occasional spelling mistakes.

When to use constants over variable or literals?

Below are the scenarios when we need to use constants instead of variable or literals:
  • If we want to use literals many times in our script,it is better to go with constants instead of variables or literals.
  • if we want to reciprocate the variable name is proper and clear way,we need to choose constants over variable or literals.

Const statement

Declares constants for use in place of literal values.

syntax:

[public|private] const cConstantName=value

public or private is the access control mechanism.

const is the keyword to define a constant.

cConstantName is the constant name.

Value is the final value.

Example:

[public|private] Const MyString = "This is my string."
[public|private] Const MyAge = 49
[public|private] Const CutoffDate = #6-1-97#
 

Note that String literal is enclosed in quotation marks (” “).

Represent Date literals and time literals by enclosing them in number signs (#).

We declare multiple constants by separating each constant name and value with a comma. For example:


Const price= 100, city= "Kolkata", x= 27
 

Coding conventions of Constants

Constants can be declared by the following way

  1. Constant name should carry the constant.
  2. use capital letters- like COLOR
  3. use con prefix or c prefix- ConColour or cColor
  4. Incase the constant needs two consecutive words then use underscore(-) to concatenate them. like cMy_Tickets
  5. Arrays should be prefixed with arr like arrMyTickets

Usage of User defined Constants

  • We can use constant to fix a value which the script will refer through out the execution cycle.like cPie=3.14
  • We can use constant to create a common title bar for massage box.

Like:

const cTitleBar="Welcome"
msgbox "How are you?",cTitleBar
msgbox "Hello there",cTitleBar

VBScript also provides a set of Inbuilt Constants:

They are as follows:

VarType Constants:

They are useful to determine the subtype of a variant.
Constant Value Description
VbEmpty 0 Uninitialized or default
VbNull 1 Contains nothing or no valid data
VbInteger 2 Subtype-Integer
VbLong 3 Subtype-Long
VbSingle 4 Subtype-Single
VbDouble 5 Subtype-Double
VbCurrency 6 Subtype-Currency
VbDate 7 Subtype-Date
VbString 8 Subtype-String
VbObject 9 Object
VbError 10 Subtype-Error
VbBoolean 11 Subtype-Boolean
VbVariant 12 Array -Variant
VbDataObject 13 Data Object
vbDecimal 14 Subtype-Decimal
vbByte 17 Subtype-Byte
vbArray 8192 Array

Tristate Constants:

These constants are useful to switch arguments:
Constant Value Description
TristateUseDefault −2 It is taken from computer default regional settings
TristateTrue −1 Denotes True
TristateFalse   0 Denotes False

String Constants:

String constants allow us to modify Strings by inserting the below constants:
Constant Value Description
VbCr Chr(13) Carriage return.
VbCrLf Chr(13) & Chr(10) Carriage return along with line feed
VbFormFeed Chr(12) Form feed.Not much useful while working with windows and Testing.
VbLf Chr(10) Line feed.very useful
VbNewLine Chr(13) or Chr(10) computer specific newline
VbNullChar Chr(0) Signifies a Character with 0 value
VbNullString String with no value it contains no value.
VbTab Chr(9) Equivalent to press a Horizontal tab.
VbVerticalTab Chr(11) Equivalent to press a Vertical tab.

Note: VbNullString is not the same as a String containing ” ” that is a space.

Color Constants:

These constants are useful for advanced scripting where we design HTML reporting for test automation. Passed test cases will take the TR color aVbGreen, Failed test cases will take VbRed whereas the Table structure will be in VbBlue or VbBlack and Warning test cases will take VbCyan.
Constant Value Description
VbBlack &h00 Black
VbRed &Hff Red
VbGreen &hFF00 Green
VbYellow &hFFFF Yellow
VbBlue &hFF0000 Blue
VbMagenta &hFF00FF Magenta
VbCyan &hFFFF00 Cyan
VbWhite &hFFFFFF White

Comparison Constants

These constants generally are used to switch between textual and binary comparison. In UFT/QTP we generally use the first two.

Constant Value Description
VbBinaryCompare 0 used to perform a binary comparison
VbTextCompare 1 used to perform a plain textual comparison.
vbUseSystemDayOfWeek 0 Used to calculate the first day of the week as per local computer
vbFirstJan1 1 Used to calculate the week in which 1st January happens to be the first week of the year.
VbFirstFourDays 2 Used to calculate the week which is having at least four first days in the new year.
VbFirstFullWeek 3 Used to calculate the first full week of the year.

Msgbox Constants:

These constants signifies that which of the buttons and icons appear on the message box, along with the default. Some of the constants are also used to determine the modality of the MsgBox.

Constant Value Description
VbOKOnly 0 Displays the Ok option only
VbOKCancel 1 Displays the OK and Cancel option
VbAbortRetryIgnore 2 Display the below options:

  • Abort
  • Retry
  • Ignore
VbYesNoCancel 3 Display the below options:

  • Yes
  • No
  • Cancel
VbYesNo 4 Display the below options:

  • Yes
  • No
VbRetryCancel 5 Display the below options:

  • Retry
  • Cancel
VbCritical 16 Displays the critical message icon
VbQuestion 32 Displays the warning query icon
VbExclamation 48 Displays the warning message icon
VbInformation 64 Displays the information message icon
VbDefaultButton1 ‘0 The first button is the default.
VbDefaultButton2 256 The second button is the default.
VbDefaultButton3 512 The third button is the default.
VbDefaultButton4 768 The fourth button is the default.
VbApplicationModal 0 It signifies a modal that means the user needs to choose some option
VbSystemModal 4096 It signifies a modal that means the user needs to choose some option and it will be always on top of the application.
VbOK 1 Tracked if Ok button is clicked
VbCancel 2 Tracked if Cancel button is clicked
VbAbort 3 Tracked if Abort button is clicked
VbRetry 4 Tracked if Retry button is clicked
VbIgnore 5 Tracked if Ignore button is clicked
VbYes 6 Tracked if Yes button is clicked
VbNo 7 Tracked if No button is clicked

Error Constant

Constant Value Description
VbObjectError 2147221504 Indicates as the base of the User defined Error number.

Date Constants

Constant Value Description
VbGeneralDate 0 Displays  date,time or both as per local system
VbLongDate 1 Display date of the local system in long format
VbShortDate 2 Display date of the local system in short format
VbLongTime 3 Display time of the local system in long format
VbShortTime 4 Display time of the local system in short format
VBSunday 1 Sunday
VBMonday 2 Monday
VBTuesday 3 Tuesday
VBWednesday 4 Wednesday
VBThrusday 5 Thrusday
VBFriday 6 Friday
VBSaturday 7 Saturday

How to use the runtime constants?

Option Explicit
Dim todatDate
todayDate=weekdays(Date())
if (todayDate=VBMonday) Then
msgbox "working day"
End if
Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 29
  • 23
  • 24
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    123
    Shares

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here