Wrox Programmer Forums
|
BOOK: Visual Basic 2005 Programmer's Reference
This is the forum to discuss the Wrox book Visual Basic 2005 Programmer's Reference by Rod Stephens; ISBN: 9780764571985
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Visual Basic 2005 Programmer's Reference section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 31st, 2008, 05:13 PM
Registered User
 
Join Date: Mar 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Parameterize the function call

Hi,

I want to know how to parameterize the function call in VBS like

Function A()
----
Function B()
----
Function C()
----

Call <any function as per the value>

Thnx
Banga


 
Old March 31st, 2008, 07:44 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

One good method is to make a variable that is a delegate--a reference to the function. Then set it to the function that you want to invoke. Here's an example:

Here are the three functions. I added a parameter to show how to handle that case in the delegate.

Private Function A(ByVal x As Integer) As Integer
    Return x + 1
End Function
Private Function B(ByVal x As Integer) As Integer
    Return x * 2
End Function
Private Function C(ByVal x As Integer) As Integer
    Return x * x
End Function

Here's the delegate definition. Variables of this type can be assigned to any function that takes an integer parameter and returns an integer result.

Private Delegate Function ABCFunction(ByVal x As Integer) As Integer

Here's the variable declaration. The variable func can refer to a function.

Dim func As ABCFunction

Finally, here's some code that uses the variable. It assigns it to point to each of the three functions and calls each.

func = AddressOf A
Debug.WriteLine("A: " & func(10))

func = AddressOf B
Debug.WriteLine("B: " & func(10))

func = AddressOf C
Debug.WriteLine("C: " & func(10))

Here are the results:

A: 11
B: 20
C: 100

I hope that helps!

Rod

Rod Stephens, Visual Basic MVP
RodStephens@vb-helper.com

*** New Book ***
"Visual Basic 2008 Programmer's Reference"

Sign up for the free VB Helper Newsletters at http://www.vb-helper.com/newsletter.html





Similar Threads
Thread Thread Starter Forum Replies Last Post
Call to function Awkuzu BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 September 19th, 2007 06:13 PM
Call C++ function from C#.NET ashooagarwal .NET Framework 1.x 0 May 24th, 2007 10:47 PM
Call a Function stealthdevil Access VBA 2 November 2nd, 2006 10:38 AM
How to call javascript function from VB function vinod_yadav1919 VB How-To 0 February 13th, 2006 06:03 AM
parameterize path? mlaba XSLT 4 October 8th, 2004 04:34 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.