Shows,paramarray,create,much,more,flexible,ca
Quick Search for:  in language:    
Shows,paramarray,create,much,more,flexible,ca
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
RentACoder Stats

 Code:  lines
 Jobs: 0 postings

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for RentACoder.
Wrapping Scrolling Text
By Paranoid_Androi d on 7/2


Create A Dummy File
By AML on 7/2


Click here to see a screenshot of this code!Captionbar manipulation!
By Peter Hebels on 7/2

(Screen Shot)

A Game Of War
By Co0nest on 7/2


Click here to see a screenshot of this code!KeyGen Example
By Bengie|NET on 7/2

(Screen Shot)

Click here to see a screenshot of this code!OpenBrowser v1.9
By Orlando Jerez on 7/2

(Screen Shot)

SendMessageBySt ring() Example
By Jaime Muscatelli on 7/2


Click here to see a screenshot of this code!FirstSunday
By Jan Paul Penning on 7/2

(Screen Shot)

Click here to see a screenshot of this code!Ikonz v1.0
By Gaurav Creations on 7/2

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

paramarray and function replacement

Print
Email
 

Submitted on: 11/7/2001 5:12:21 AM
By: Izek 
Level: Intermediate
User Rating: By 24 Users
Compatibility:

Users have accessed this article 4661 times.
 
(About the author)
 
     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

 
 
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
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
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
11/9/2001 1:40:14 AM:chillin
:]
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/10/2001 1:23:59 AM:shel
sup phrosty love ya
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | RentACoder Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.