Search Tools Links Login

paramarray and function replacement


Visual Basic 6, or VB Classic

1. Shows you how to use paramarray to create much more flexible and capable functions
2. Shows you how to REPLACE vb's existing functions

Original Author: Izek

Code




In this tutorial I will show you how to write your a function that can accept ANY number of parameters and how to replace existing vb functions so that when you call function Right for example it will execute YOUR version of the function instead of vb's version.



As some of you may or may not know VB has a function called SWITCH


Function Switch(ParamArray VarExpr() As Variant) As Variant


That function evaluates a list of expressions and returns a Variant value or an expression associated with the first expression in the list that is True. Meaning you can do something like. . .



Dim i as integer


Dim retval as boolean


i = 1


retval = Switch(i = 1, True, i = 2, False)



When you execute that it will return true because i is 1, if i was 2 it would return false, but if i was 3 it will ERROR!!!!!!!!! because none of the expressions evaluated to true.


Also another thing about this function is that you can pass as many parameters as you want and that is what makes it so special for our purposes.



What I decided to do was to REPLACE VB's existing switch function with my own switch function so that when i call the switch function and none of the expressions evaluate to true it will either return "" OR it will return the "default parameter".



Now, to explain how it works and what the default parameter is . . .




Function Switch(ParamArray VarExpr() As Variant) As Variant


'paramarray makes it so that you can pass as many parameters as you want which can be accessed using the VarExpr array.


Dim i As Integer


For i = 0 To UBound(VarExpr) Step 2


'this loop will go through every other argument in our parameter array also note, when we pass argument like i = 1 VarExpr for that argument will not be "i = 1" it will be True if i is 1 or it will be False if its not


If VarExpr(i) = True Then


'check to see if argument evaluated to true


Switch = VarExpr(i + 1)


'return the value for that argument


Exit Function


End If


Next i


'if none of the arguments evaluted to true this part will check if you have even or odd number of parameters if you have ODD number of parameters it will assume that the last parameter is the default parameter which is to be returned if nothing evaluted to true


If (UBound(VarExpr) + 1) Mod 2 = 1 Then


Switch = VarExpr(UBound(VarExpr))


'return the last ("default") paramter


End If


End Function



Also note that our function name is Switch just like VB's function name, we put our function in a module so that when you call Switch function it will call your(more flexible) version of the function and not VB's default version.
Now when we call our new function . . .


Dim i as integer


Dim retval as string


i = 1


retval = Switch(i = 1, True, i = 2, False)



retval will be "True", but if we take out i = 1 it will return ""



Dim i as integer


Dim retval as string


retval = Switch(i = 1, True, i = 2, False)



because i is 0 and none of the expressions evaluted to true, but what we can do is add the extra "default" parameter


Dim i as integer



Dim retval as string


retval = Switch(i = 1, True, i = 2, False, Now)



now when the function will execute it will return current time and date (because thats what function now returns) because none of the expressions evaluated to true




If you do not understand the above explation and want a more indetail explanation and/or example you can contact me at



email: izek.programmer@verzion.net


aim: ozik13


icq: 53982424


Please leave feedback :)

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 111 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.