The IIf() function can also be used in place of an If...Then...ElseIf scenario. When the function is called, the Expression is checked. As we saw already, if the expression is true, the function returns the value of the TruePart argument and ignores the last argument. To use this function as an alternative to If...Then...ElseIf statement, if the expression is false, instead of immediately returning the value of the FalsePart argument, you can translate that part into a new IIf function. The pseudo-syntax would become: Public Function IIf( _ ByVal Expression As Boolean, _ ByVal TruePart As Object, _ Public Function IIf( _ ByVal Expression As Boolean, _ ByVal TruePart As Object, _ ByVal FalsePart As Object _ ) As Object ) As Object In this case, if the expression is false, the function returns the TruePart and stops. If the expression is false, the compiler accesses the internal IIf function and applies the same scenario. Here is example: Sub Exercise() Dim MemberAge As Byte Dim MembershipCategory As String MemberAge = 74 MembershipCategory = _ IIf(MemberAge <= 18, "Teen", IIf(MemberAge < 55, "Adult", "Senior")) MsgBox ("Membership: " & MembershipCategory) End Sub We saw that in an If...Then...ElseIf statement you can add as many ElseIf conditions as you want. In the same, you can call as many IIf functions in the subsequent FalsePart sections as you judge necessary: Public Function IIf( _ ByVal Expression As Boolean, _ ByVal TruePart As Object, _ Public Function IIf( _ ByVal Expression As Boolean, _ ByVal TruePart As Object, _ Public Function IIf( _ ByVal Expression As Boolean, _ ByVal TruePart As Object, _ Public Function IIf( _ ByVal Expression As Boolean, _ ByVal TruePart As Object, _ ByVal FalsePart As Object _ ) As Object ) As Object ) As Object ) As Object
As we have seen so far, the Choose function takes a list of arguments. To use it as an alternative to the If...Then...ElseIf...ElseIf condition, you can pass as many values as you judge necessary for the second argument. The index of the first member of the second argument would be 1. The index of the second member of the second argument would be 2, and so on. When the function is called, it would first get the value of the first argument, then it would check the indexes of the available members of the second argument. The member whose index matches the first argument would be executed. Here is an example: Sub Exercise() Dim Status As Byte, EmploymentStatus As String Status = 3 EmploymentStatus = Choose(Status, _ "Full Time", _ "Part Time", _ "Contractor", _ "Seasonal") MsgBox ("Employment Status: " & EmploymentStatus) End Sub This would produce: So far, we have used only strings for the values of the second argument of the Choose() function. In reality, the values of the second argument can be almost anything. One value can be a constant. Another value can be a string. Yet another value can come from calling a function. Here is an example: Private Function ShowContractors$() ShowContractors$ = "=-= List of Contractors =-=" & vbCrLf & _ "Martin Samson" & vbCrLf & _ "Geneviève Lam" & vbCrLf & _ "Frank Viel" & vbCrLf & _ "Henry Rickson" & vbCrLf & _ "Samuel Lott" End Function Sub Exercise() Dim Status As Byte, Result$ Status = 3 Result = Choose(Status, _ "Employment Status: Full Time", _ "Employment Status: Part Time", _ ShowContractors, _ "Seasonal Employment") MsgBox (Result) End Sub This would produce: The values of the second argument can even be of different types.
The Switch() function is a prime alternative to the If...Then...ElseIf...ElseIf condition. The argument to this function is passed as a list of values. As seen previously, each value is passed as a combination of two values: ConditionXToCheck, StatementX As the function is accessed, the compiler checks each condition. If a condition X is true, its statement is executed. If a condition Y is false, the compiler skips it. You can provide as many of these combinations as you want. Here is an example: Private Enum EmploymentStatus FullTime PartTime Contractor Seasonal End Enum Sub Exercise() Dim Status As EmploymentStatus Dim Result As String Status = EmploymentStatus.Contractor Result = "Unknown" Result = Switch(Status = EmploymentStatus.FullTime, "Full Time", _ Status = EmploymentStatus.PartTime, "Part Time", _ Status = EmploymentStatus.Contractor, "Contractor", _ Status = EmploymentStatus.Seasonal, "Seasonal") MsgBox ("Employment Status: " & Result) End Sub This would produce: In a true If...Then...ElseIf...ElseIf condition, we saw that there is a possibility that none of the conditions would fit, in which case you can add a last Else statement. The Switch() function also supports this situation if you are using a number, a character, or a string. To provide this last alternative, instead of a ConditionXToCheck expressionk, enter True, and include the necessary statement. Here is an example: Sub Exercise() Dim Status As Byte Dim Result As String Status = 12 Result = Switch(Status = 1, "Full Time", _ Status = 2, "Part Time", _ Status = 3, "Contractor", _ Status = 4, "Seasonal", _ True, "Unknown") MsgBox ("Employment Status: " & Result) End Sub This would produce: Remember that you can also use True with a character. Here is an example: Sub Exercise() Dim Gender As String Dim Result As String Gender = "H" Result = Switch(Gender = "f", "Female", _ Gender = "F", "Female", _ Gender = "m", "Male", _ Gender = "M", "Male", _ True, "Unknown") MsgBox ("Gender: " & Result) End Sub This would produce:
|
|
||||||||||||
|