|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:
- User defined or named constants or custom constants
- 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.
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?
- 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.
Declares constants for use in place of literal values.
[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.
[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
- Constant name should carry the constant.
- use capital letters- like COLOR. It is a general convention. And constants are easy to differentiate amongst the other variables.
- use con prefix or c prefix- ConColour or cColor
- Incase the constant needs two consecutive words then use underscore(-) to concatenate them. like cMy_Tickets
- 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.
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.
VBScript also provides a set of Inbuilt Constants:
They are as follows:
|VbEmpty||0||Uninitialized or default|
|VbNull||1||Contains nothing or no valid data|
|TristateUseDefault/VbUseDefault||−2||It is taken from computer default regional settings|
|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.
These constants generally are used to switch between textual and binary comparison. In UFT/QTP we generally use the first two.
|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.|
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.
|VbOKOnly||0||Displays the Ok option only|
|VbOKCancel||1||Displays the OK and Cancel option|
|VbAbortRetryIgnore||2||Display the below options:|
|VbYesNoCancel||3||Display the below options:|
|VbYesNo||4||Display the below options:|
|VbRetryCancel||5||Display the below options:|
|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|
|VbObjectError||2147221504||Indicates as the base of the User defined Error number.|
|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|
|VBUseSystem||0||Displays 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