Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

call a function by run-time generated name

Posted on 2005-03-02
13
Medium Priority
?
1,043 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

581 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