Solved

calling any function in a class in vb.net

Posted on 2006-11-29
7
252 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Please help| Getting the syntax error in below query. 2 70
ejb stateless example 2 42
VB.net and sql server 4 54
youtube blocking politics 4 79
In this post we will learn different types of Android Layout and some basics of an Android App.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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…

756 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