To assist you with checking a condition and its alternative,
the Visual Basic language provides a function named IIf. Its syntax is:
Public Function IIf( _ ByVal Expression As Boolean, _ ByVal TruePart As Variant, _ ByVal FalsePart As Variant _ ) As Variant
This function operates like an If...Then...Else
condition. It takes three required arguments and returns a result of type Variant.
This returned value will hold the result of the function.
The condition to check is passed as the first argument:
As mentioned already, you can retrieved the value of the
right argument and assign it to the result of the function. The expression we
saw early can be written as follows:
Sub Exercise()
Dim MemberAge As Integer
Dim MemberCategory As String
MemberAge = 16
MemberCategory = IIf(MemberAge <= 18, "Teen", "Adult")
MsgBox ("Membership: " & MemberCategory)
End Sub
This would produce the same result we saw earlier.
We have learned how to check whether a condition is True or
False and take an action. Here is an example:
Sub Exercise()
Dim Status As Integer, EmploymentStatus As String
Status = 1
EmploymentStatus = "Unknown"
If Status = 1 Then
EmploymentStatus = "Full Time"
End If
MsgBox ("Employment Status: " & EmploymentStatus)
End Sub
To provide an alternative to this operation, the Visual Basic language provides a function
named Choose. Its syntax
is:
Public Function Choose( _ ByVal Index As Double, _ ByVal ParamArray Choice() As Variant _ ) As Variant
This function takes two required arguments. The fist
argument is equivalent to the ConditionToCheck of our If...Then formula. For the Choose() function, this first argument must be a number. This is the value against which the second argument will be
compared. Before calling the function, you must know the value of the first
argument. To take care of this, you can first declare a variable and initialize
it with the desired value. Here is an example:
Sub Exercise()
Dim Status As Byte, EmploymentStatus As String
Status = 1
EmploymentStatus = Choose(Status, ...)
MsgBox ("Employment Status: " & EmploymentStatus)
End Sub
The second argument can be the Statement of our
formula. Here is an example:
Choose(Status, "Full Time")
We will see in the next sections that the second argument is
actually a list of values and each value has a specific position referred to as
its index. To use the function in an If...Then scenario, you pass only
one value as the second argument. This value/argument has an index of
1. When the Choose() function
is called in an If...Then implementation, if the first argument holds a
value of 1, the second argument is validated.
When the Choose() function has been called, it returns a
value of type Variant. You can retrieve that value, store it in
a variable and use it as you see fit. Here is an example:
Sub Exercise()
Dim Status As Byte, EmploymentStatus As String
Status = 1
EmploymentStatus = Choose(Status, "Full Time")
MsgBox ("Employment Status: " & EmploymentStatus)
End Sub
This would produce:
In some cases, the Choose() function can produce a null result. Consider the same program we used
earlier but with a different value:
Module Exercise
Sub Exercise
Dim Status As Integer, EmploymentStatus As String
Status = 2
EmploymentStatus = Choose(Status, "Full Time")
MsgBox(EmploymentStatus)
Return 0
End Function
End Module
This would produce an error because there is no value in
index 2 after the Status variable has been initialized with 2. To use this function as an alternative to the
If...Then...Else operation, you
can pass two values for the second argument. The second argument is actually
passed as a list of values. Each value has a specific position as its index. To
use the function in an If...Then...Else implementation, pass two values
for the second argument. Here is an example:
Choose(Status, "Full Time", "Part Time")
The second argument to the function, which is the first
value of the Choose argument, has an index of 1. The third argument to
the function, which is the second value of the Choose
argument, has an index of 2.
When the Choose() function is called, if the first
argument has a value of 1, then the second argument is validated. If the first
argument has a value of 2, then the third argument is validated. As mentioned
already, you can retrieve the returned value of the function and use it however you
want. Here is an example:
Sub Exercise()
Dim Status As Integer, EmploymentStatus As String
Status = 2
EmploymentStatus = Choose(Status, "Full Time", "Part Time")
MsgBox ("Employment Status: " & EmploymentStatus)
End Sub
This would produce:
As another alternative to an If...Then
condition, the Visual Basic language provides a function named Switch.
Its syntax is:
Public Function Switch( _ ByVal ParamArray VarExpr() As Variant _ ) As Variant
This function takes one required argument. To use it in an If...Then
scenario, pass the argument as follows:
Switch(ConditionToCheck, Statement)
In the ConditionToCheck placeholder, pass a Boolean
expression that can be evaluated to True or False. If that condition is true,
the second argument would be executed.
When the Switch() function has been called, it
produces a value of type Variant (such as a string) that you can use as
you see fit. For example, you can store it in a variable. Here is an example:
Sub Exercise()
Dim Status As Integer, EmploymentStatus As String
Status = 1
EmploymentStatus = "Unknown"
EmploymentStatus = Switch(Status = 1, "Full Time")
MsgBox ("Employment Status: " & EmploymentStatus)
End Sub
In this example, we used a number as argument. You can also
use another type of value, such as an enumeration. Here is an example:
Private Enum EmploymentStatus FullTime PartTime Contractor Seasonal Unknown End Enum Sub Exercise() Dim Status As EmploymentStatus Dim Result As String Status = EmploymentStatus.FullTime Result = "Unknown" Result = Switch(Status = EmploymentStatus.FullTime, "Full Time") MsgBox ("Employment Status: " & Result) End Sub
When using the Switch function, if you call it with a value that is
not checked by the first argument, the function produces an error. To apply this function to an
If...Then...Else scenario, you can call it
using the following formula:
Switch(Condition1ToCheck, Statement1, Condition2ToCheck, Statement2)
In the Condition1ToCheck placeholder, pass a Boolean
expression that can be evaluated to True or False. If that condition is true,
the second argument would be executed. To provide an alternative to the first
condition, pass another condition as Condition2ToCheck. If the Condition2ToCheck
is true, then Statement2 would be executed. Once gain, remember that you
can get the value returned by the Switch function and use it. Here is an
example:
Private Enum EmploymentStatus FullTime PartTime Contractor Seasonal Unknown End Enum Sub Exercise() Dim Status As EmploymentStatus Dim Result As String Status = EmploymentStatus.PartTime Result = "Unknown" Result = Switch(Status = EmploymentStatus.FullTime, "Full Time", _ Status = EmploymentStatus.PartTime, "Part Time") MsgBox ("Employment Status: " & Result) End Sub
This would produce:
|
Introduction to Conditional Statements
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment