Solved

calling any function in a class in vb.net

Posted on 2006-11-29
7
248 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEvens challenge 6 63
ClickOnce Install - Shortcut Question 3 59
noX challenge 17 77
WMI, model #, retrieving information 10 94
A short article about a problem I had getting the GPS LocationListener working.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

760 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

19 Experts available now in Live!

Get 1:1 Help Now