Thursday

QTP - Naming Convention in VBS

Copyright: Roman Koch · Nutzungsbestimmungen · Datenschutz
Source: http://www.romankoch.ch/capslock/vbnaming.htm

Naming Conventions for Visual Basic Scripts

Basic Principles
Compared to Visual Basic and VBA, VBScript is less restrictive in the way it deals with variables and constants. VBScript knows only one basic data type, the Variant, and you can easily use the same variable to store a number and then a string later on. You don't even need to declare your variables. Although this is great for prototyping, I have found that this makes script maintenance difficult, and can become the cause of program errors or strange program behaviours. Therefore, on top of my variable naming conventions, two basic principles always apply:

Enforce declarations. If a VBScript starts with Option Explicit statement, all variables and constants must be explicitly declared, or an error occurs. Option Explicit prevents you from lazy programming.
Never misuse variables. Once you have named a variable to be used for a specific data type, never misuse it for another data type. For example, if you have a variable named iClicks to count the number of clicks, never assign a string to it - even if you know that the variable is currently "unused".
Data Type Prefixes
By using data type prefixes, your variables always indicate what they are designed for. I'm using just six prefixes:

Prefix Data Types Example
b Boolean (true or false) bChecked = True
d Date/time dStartTime = Now
i All numeric data types
(Byte, Integer, Currency, Long, Single, Double) iTax = iPrice * 0.076
o Objects Set oFile = Nothing
s Strings sTitle = "Welcome!"
u User interface elements uPara.style.display = "block"

Constants Prefix
I have found that I usually have only an handful of constants in my scripts. Indicating the data type therefore is less important for constants. I'm using the prefix letter c for all constants, e.g.

Const cTitle = "Welcome!"
uDocument.title = cTitle
Scope of Variables and Constants
Variables and constants can be declared on the script level, meaning they are available to all Sub and Function procedures in a script, or on the procedure level, meaning they are available only to the Sub or Function procedure that contains the declaration. In general, I try to avoid script- level declarations, as it makes long scripts difficult to read. Use script-level declarations only if you really need a variable or constant in multiple procedures - don't do it just to save memory.

For script-level variables and constants, I just double the prefix. sTitle is a procedure-level string variable, while ccPath is a script-level constant.

Best Practices
In addition to the basic principles and naming prefixes, the following best practices help me to avoid run-time errors and to keep my code easy to maintain:

Always pass variable by value. VBScript by default passes variables to functions and procedures by reference, and in most cases, this default is not what you would expect. By explicitly passing variables by value, you can avoid unexpected changes to your variables.

Function GetBetter (ByVal iNumber)
iNumber = iNumber + 1
GetBetter = iNumber
End Function
iRating = 4
iNewRating = GetBetter(iRating)
Initialize variables. VB does not require you to initialize variables and usually behaves consistently when using an uninitialized variable. However, by explicitly initializing variables, I can later on remember how I wanted my script to work when I wrote it. Instead of

Dim iRecords
...
iRecords = iRecords + 1
I prefer to write

Dim iRecords
iRecords = 0
...
iRecords = iRecords + 1
Use conversion functions. For calculations, I rely on the VB subtype conversion functions to document my code. When processing numeric user input, the short and dangerous way of coding is

iRating = uRating.valueI prefer to write

If IsNumeric(uRating.value) Then
iRating = CInt(uRating.value)
Else
...
End If
Use predefined constants. Many objects provide predefined constants for VB and VBA. The FileSystemObject for example provides constants that indicate the opening mode for files. Instead of

Set oLog = oFSO.OpenTextFile(Server.MapPath("log.txt"), 8, True)
I prefer to write

Const fsoForAppending = 8
Set oLog = oFSO.OpenTextFile(Server.MapPath("log.txt"), fsoForAppending, True)
Use short numeric variables for local counters only. Traditional programming uses short numeric variables, especially i and j, as a local buffer. Although they do not conform to my naming conventions, I still use them, but only for local (procedure-level) counters:

Dim i, j
For i = 0 to iRows - 1
For j = 0 to iCells - 1
uTable.rows(i).cells(j).innertext = ""
Next
Next

User Define Function

' Object pass karna jaroori hai
RegisterUserFunc "WebEdit","set", "mthd"

Browser("CASO Dashboard").Page("CASO Dashboard").WebEdit("WebEdit").set "copy"

Function mthd (obj, x)
Msgbox x
'
If x="copy" Then
Msgbox "Hi"
End If

' dim y
'
' y = obj.GetROProperty("value")
'
' Reporter.ReportEvent micDone, "previous value", y
'
' MySet=obj.Set(x)

End Function

UnRegisterUserFunc "WebEdit", "Set"

Wednesday

Adding a Quote in vbs

StringObjMth=ObjectName & "(" & Chr(34) & Method & Chr(34) & ")"


Chr(34) will give value , """ can be mislead.