article

paramarray and function replacement

Email
Submitted on: 1/20/2015 10:38:00 PM
By: Izek (from psc cd)  
Level: Intermediate
User Rating: By 25 Users
Compatibility: VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 1170
 
     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

 
				

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 :)


Other 6 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.