Solved

calling any function in a class in vb.net

Posted on 2006-11-29
7
249 Views
Last Modified: 2010-04-16
we have a class that has many functions, some with arguments, some not.  we build an array of this class and need to be able to tell the particular instance of the class to perform a desired function.  is there a method to call a desired function within a class with a variable refering to the function?  the class has a function:

public function play(number as integer)
 ...
end function

in our main code we call the function with:

gClass(i).play(number)

or any number of other functions within the class.

is there a way to send a vaiable for the function name like:

dim var as string = "play"
gClass(i).var(number)

this method fails, of course.  is there a way to do this?

do we need to set up a separate function that calls the other functions?

public function command(var as string)
 dim mess() as string=split(var,vblf)
 select case mess(0)
  case "play"
    play(mess(1))
 end case
end function

if this is all we can do, then we are going to extend our code quite a bit and it seems unnecessary.

help!
0
Comment
Question by:ddepuemd
7 Comments
 

Expert Comment

by:axnst2
ID: 18037884
Use function pointers and just have an array of function pointers.  This way each function would have a number (the array index) associated with it.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18037919
You can use CallByName() or Reflection.

I have to drop the kids off at school though.

If no one else gives an example before I return then I can post one for ya...
0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 18038172
Here's an example of using the CallByName function that idle mind suggested (assuming your function Play returns an integer):

        Dim FuncName As String = "Play"
        Dim FuncParam As Integer = 10

        Dim Result As Integer = Convert.ToInt32(CallByName(gClass(i), FuncName, CallType.Method, FuncParam))
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18038229
Thanx Zephyr.

Here is an example of CallByName() and Reflection:

    Public Class SomeClass
        Public Function play(ByVal number As Integer)
            Return number
        End Function
    End Class

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim method As String = "play"
        Dim parameter As Integer = 411

        Dim sc As New SomeClass

        Dim ret As Integer = CallByName(sc, method, CallType.Method, parameter)
        MsgBox(ret)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim method As String = "play"
        Dim parameter As Integer = 411

        Dim sc As New SomeClass

        Dim ret As Integer = sc.GetType.GetMethod(method).Invoke(sc, New Object() {parameter})
        MsgBox(ret)
    End Sub
0
 

Author Comment

by:ddepuemd
ID: 18038744
How do you guys remember all those functions????  Anyway, like I said, some of the functions and methods have parameters, some don't.  If I call the method like you have indicated:

Dim ret As Integer = CallByName(sc, method, CallType.Method, parameter)

I get an error on some that do not have arguments.  How do I get around that?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 18038851
Not the prettiest solution...

        Dim ret As Integer
        Try
            ret = CallByName(sc, method, CallType.Method, parameter)
        Catch ex As Exception
            ret = CallByName(sc, method, CallType.Method)
        End Try
        MsgBox(ret)

Or:

        Dim ret As Integer
        Try
            ret = sc.GetType.GetMethod(method).Invoke(sc, New Object() {parameter})
        Catch ex As Exception
            ret = sc.GetType.GetMethod(method).Invoke(sc, Nothing)
        End Try
        MsgBox(ret)
0
 

Author Comment

by:ddepuemd
ID: 18039571
Isnt' there any way to tell how many parameters are required for the method called?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
A short article about a problem I had getting the GPS LocationListener working.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now