Sunday, July 19, 2009

Select Case Statement in VB Script of QTP?

Executes one of several groups of statements, depending on the value of an expression.

Select Case testexpression
[Case expressionlist-n
[statements-n]] . . .
[Case Else expressionlist-n
End Select

Any numeric or string expression.
Required if Case appears. Delimited list of one or more expressions.
One or more statements executed if testexpression matches any part of expressionlist-n.
One or more statements executed if testexpression doesn't match any of the Case clauses.
If testexpression matches any Case expressionlist expression, the statements following that Case clause are executed up to the next Case clause, or for the last clause, up to End Select. Control then passes to the statement following End Select. If testexpression matches an expressionlist expression in more than one Case clause, only the statements following the first match are executed.

The Case Else clause is used to indicate the elsestatements to be executed if no match is found between the testexpression and an expressionlist in any of the other Case selections. Although not required, it is a good idea to have a Case Else statement in your Select Case block to handle unforeseen testexpression values. If no Case expressionlist matches testexpression and there is no Case Else statement, execution continues at the statement following End Select.

Select Case statements can be nested. Each nested Select Case statement must have a matching End Select statement.

The following example illustrates the use of the Select Case statement.

Dim arr(5)
arr(0) = "The"
arr(1) = "World"
arr(2) = "is"
arr(3) = "not"
arr(4) = "good"
arr(5) = "enough"
i=inputbox("enter value for i")

Select Case i
Case 0 :
msgbox arr(0)
Case 1 :
msgbox arr(1)
Case 2 :
msgbox arr(2)
Case 3 :
msgbox arr(3)
Case 4 :
msgbox arr(4)
Case 5 :
msgbox arr(5)
case else :
msgbox "i come if there is no match for your input"

End Select

1 comment: