Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

calling any function in a class in vb.net

Posted on 2006-11-29
7
Medium Priority
?
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 86

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 86

Accepted Solution

by:
Mike Tomlinson earned 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 …

715 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