call a function by run-time generated name

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
LVL 1
UnFiReAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

coopzzCommented:
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
UnFiReAuthor Commented:
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
coopzzCommented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

coopzzCommented:
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
UnFiReAuthor Commented:
what would it do? I do not really understand ...
0
checooCommented:
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
coopzzCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
checooCommented:
you might possibly want to have a look at

http://samples.gotdotnet.com/quickstart/howto/doc/Invoke.aspx
0
coopzzCommented:
geees checoo

if you had read the thread you will notice I had already posted that web link.
0
checooCommented:
sorry about that...i missed that section
0
coopzzCommented:
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
checooCommented:
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
UnFiReAuthor Commented:
Ok, thank you guys,
I'll split points - both answers are very good.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.