Solved

calling any function in a class in vb.net

Posted on 2006-11-29
7
250 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

776 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