|
| | Terms of Agreement:
By using this article, you agree to the following terms...
1) You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description. |
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 :) | | | Report Bad Submission | | | Your Vote! |
See Voting Log | | Other User Comments | 11/9/2001 1:40:14 AM:chillin :]
| 11/10/2001 1:23:59 AM:shel sup phrosty love ya
| 1/14/2002 1:53:40 PM:Matt Roberts Good job! I have wondered about this
for a long time and somehow totally
missed ParamArray! Thanks...keep it up!
| | Add Your Feedback! | Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.
NOTICE: The author of this article has been kind enough to share it with you. If you have a criticism, please state it politely or it will be deleted.
For feedback not related to this particular article, please click here. | | |