?
Solved

call a function by run-time generated name

Posted on 2005-03-02
13
Medium Priority
?
1,040 Views
Last Modified: 2008-02-01
Hey experts!

I have 3 functions:
function1
function2
function3

How can i call any of those by their name that is specified in the runtime
Let's say something like this

myVariable = "function" & n 'where n is the number of function
0
Comment
Question by:UnFiRe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
13 Comments
 
LVL 11

Expert Comment

by:coopzz
ID: 13445523
why don't you just use a select statement to process the n functions ie:
-----------
myVarible = gotoFunction(n)
----------
private function gotoFunction( n as integer ) as object
    select case (n)
        case 1: return function1()
        case 2: return function2()
        case 3: return function3()
    end select
end function

Cheers

CooPzZ
0
 
LVL 1

Author Comment

by:UnFiRe
ID: 13445555
i used the simpliest example to show you what my need is.
In reality, i do not use function1 function2 or function3.
I use variables and functions. There is many of them and the select case statement's will not do the thing.
I need to know how to call a function by the name that can be composed in run-time by combining several values - just like concatinating a string. I know there was an option to do this in VB 6 and there is an option to do that in other languages.
0
 
LVL 11

Expert Comment

by:coopzz
ID: 13445556
And I think this is the new vb6:callbyname way of doing it in .net
http://samples.gotdotnet.com/quickstart/howto/doc/Invoke.aspx
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:coopzz
ID: 13445587
sorry I figured they must have taken it out but they don't appear to have..

callbyname function still works as in
---------------
Dim i As Integer = 100
Dim o As Object
o = CallByName(i, "ToString", CallType.Method)
0
 
LVL 1

Author Comment

by:UnFiRe
ID: 13445791
what would it do? I do not really understand ...
0
 
LVL 9

Assisted Solution

by:checoo
checoo earned 400 total points
ID: 13446517
you should look at "reflection" for a solution to your problem, something like below should help

Function InvokeMethod(ByVal obj As Object, ByVal methodName As String, ByVal throwIfError As Boolean, ByVal ParamArray args() As Object) As Object
    Try
        Return obj.GetType().InvokeMember(methodName, BindingFlags.Instance Or BindingFlags.InvokeMethod Or BindingFlags.Public, Nothing, obj, args)
    Catch ex As Exception
        If throwIfError Then Throw ex
    End Try
    ' if method doesn't exists or throws
    Return Nothing
End Function
0
 
LVL 11

Accepted Solution

by:
coopzz earned 400 total points
ID: 13446617
Well just say that you have a form that your using with a procedure
heres an example, just create a new webform.asp call it anything and add this to the code behind.
------------------------------
Public Class Form1
    Inherits System.Web.UI.Page

    Public Sub DoThis1(s as string)
        Response.write(s)
    End Sub
    Public Sub DoThis2(s as string)
        Response.write("no.2: " & s)
    End Sub

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dim nS as string = "1"
        callbyname( me, "DoThis" & nS, CallType.Method, "Hello World" )
    End Sub

End Class
----------------------
CooPzZ
0
 
LVL 9

Expert Comment

by:checoo
ID: 13446646
you might possibly want to have a look at

http://samples.gotdotnet.com/quickstart/howto/doc/Invoke.aspx
0
 
LVL 11

Expert Comment

by:coopzz
ID: 13446673
geees checoo

if you had read the thread you will notice I had already posted that web link.
0
 
LVL 9

Expert Comment

by:checoo
ID: 13446685
sorry about that...i missed that section
0
 
LVL 11

Expert Comment

by:coopzz
ID: 13446707
Thats alright.. whats the benefit of doing to through reflection anyway..?
 as far as I can tell callbyname function does the job still nice and easy
0
 
LVL 9

Expert Comment

by:checoo
ID: 13446759
CallByname uses reflection internally

the following is from online help

Note   While the CallByName function may be useful in some situations, you need to weigh its usefulness against the performance implications — using CallByName to invoke a procedure is slightly slower than a late-bound call. If you are invoking a function that is called repeatedly, such as inside a loop, CallByName can have a severe effect on performance.
0
 
LVL 1

Author Comment

by:UnFiRe
ID: 13446806
Ok, thank you guys,
I'll split points - both answers are very good.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question