What are VBScript Constants?

VbScript Constants
VbScript Constants
VbScript Constants

VbScript 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:

Ads code goes here
  1. User defined or named constants or custom constants
  2. Built in Constant

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 constant’s value can not be changed during the execution time.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.

  • A named constant is similar to variable as it represent some value but its value can not be changed at runtime.
  • A variable is transient but named constant is static.

Note: Constant value cannot 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.
READ  How to Use VBScript Variables?

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. Value is the final value. However the public or private keyword is optional though.

cConstantName is the constant name.

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

Scope of constants

a constant declared at the script level has script level scope and a constant declared inside the procedure is having procedure level scope.

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. It is a general convention. And constants are easy to differentiate amongst the other variables.
  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

When to use named constant and when to use literals?

  • Named constants can reduce the defects in your application as you need to mention literals several times and chances are there that they may be misspelled and can be executed unnoticed.
  • Named constants increase clarity over literals.
  • Named constants are short, clear and crisp hence easy to maintain,read and debug.
READ  Difference between Dim and Redim Statement In VBScript,UFT,QTP

VBScript also provides a set of Inbuilt Constants:

They are as follows:

VarType Constants:

They are useful to determine the subtype of a variant.
ConstantValueDescription
VbEmptyUninitialized or default
VbNull1Contains nothing or no valid data
VbInteger2Subtype-Integer
VbLong3Subtype-Long
VbSingle4Subtype-Single
VbDouble5Subtype-Double
VbCurrency6Subtype-Currency
VbDate7Subtype-Date
VbString8Subtype-String
VbObject9Object
VbError10Subtype-Error
VbBoolean11Subtype-Boolean
VbVariant12Array -Variant
VbDataObject13Data Object
vbDecimal14Subtype-Decimal
vbByte17Subtype-Byte
vbArray8192Array

Tristate Constants:

These constants are useful to switch arguments:
ConstantValueDescription
TristateUseDefault/VbUseDefault−2It is taken from computer default regional settings
TristateTrue/VbTrue−1Denotes True
TristateFalse/VbFalse  0Denotes False

String Constants:

String constants allow us to modify Strings by inserting the below constants:
ConstantValueDescription
VbCrChr(13)Carriage return.
VbCrLfChr(13) & Chr(10)Carriage return along with line feed
VbFormFeedChr(12)Form feed.Not much useful while working with windows and Testing.
VbLfChr(10)Line feed.very useful
VbNewLineChr(13) or Chr(10)computer specific newline
VbNullCharChr(0)Signifies a Character with 0 value
VbNullStringString with no valueit contains no value.
VbTabChr(9)Equivalent to press a Horizontal tab.
VbVerticalTabChr(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.
ConstantValueDescription
VbBlack&h00Black
VbRed&HffRed
VbGreen&hFF00Green
VbYellow&hFFFFYellow
VbBlue&hFF0000Blue
VbMagenta&hFF00FFMagenta
VbCyan&hFFFF00Cyan
VbWhite&hFFFFFFWhite

Comparison Constants

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

ConstantValueDescription
VbBinaryCompareused to perform a binary comparison
VbTextCompare1used to perform a plain textual comparison.
vbUseSystemDayOfWeekUsed to calculate the first day of the week as per local computer
vbFirstJan11Used to calculate the week in which 1st January happens to be the first week of the year.
VbFirstFourDays2Used to calculate the week which is having at least four first days in the new year.
VbFirstFullWeek3Used to calculate the first full week of the year.
READ  Learn File System Object(FSO) in UFT or QTP or VBScript Free

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.

ConstantValueDescription
VbOKOnlyDisplays the Ok option only
VbOKCancel1Displays the OK and Cancel option
VbAbortRetryIgnore2Display the below options:

  • Abort
  • Retry
  • Ignore
VbYesNoCancel3Display the below options:

  • Yes
  • No
  • Cancel
VbYesNo4Display the below options:

  • Yes
  • No
VbRetryCancel5Display the below options:

  • Retry
  • Cancel
VbCritical16Displays the critical message icon
VbQuestion32Displays the warning query icon
VbExclamation48Displays the warning message icon
VbInformation64Displays the information message icon
VbDefaultButton1‘0The first button is the default.
VbDefaultButton2256The second button is the default.
VbDefaultButton3512The third button is the default.
VbDefaultButton4768The fourth button is the default.
VbApplicationModalIt signifies a modal that means the user needs to choose some option
VbSystemModal4096It signifies a modal that means the user needs to choose some option and it will be always on top of the application.
VbOK1Tracked if Ok button is clicked
VbCancel2Tracked if Cancel button is clicked
VbAbort3Tracked if Abort button is clicked
VbRetry4Tracked if Retry button is clicked
VbIgnore5Tracked if Ignore button is clicked
VbYes6Tracked if Yes button is clicked
VbNo7Tracked if No button is clicked

Error Constant

ConstantValueDescription
VbObjectError2147221504Indicates as the base of the User defined Error number.

Date Constants

ConstantValueDescription
VbGeneralDateDisplays  date,time or both as per local system
VbLongDate1Display date of the local system in long format
VbShortDate2Display date of the local system in short format
VbLongTime3Display time of the local system in long format
VbShortTime4Display time of the local system in short format
VBSunday1Sunday
VBMonday2Monday
VBTuesday3Tuesday
VBWednesday4Wednesday
VBThrusday5Thrusday
VBFriday6Friday
VBSaturday7Saturday
VBUseSystemDisplays the date format as per your computer

How to use the runtime constants?

Option Explicit
Dim todatDate
todayDate=weekdays(Date())
if (todayDate=VBMonday) Then
msgbox "working day"
End if

 

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *