tags:

views:

45

answers:

3

Hi I am using following code to run a private function. I have two values in my combo box, One and Two and two private functions with the same names, Private Sub One() and Private Sub Two()

I want my application to call the function whatever value user choses in the combo box. If One is chosen in the combo box, Private function one should be called. Thanks Code is below, that does not work

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim vrValue = ComboBox1.Items(1)

    Call vrValue()' In this case vrValue is Two, so Two() should be called.
End Sub
Private Sub two()
    MsgBox("Function called")
End Sub
A: 
Dim vrValue = ComboBox1.SelectedItem.ToString()

Select vrValue
    Case "One"
        One()
    Else
        Two()
End Select
A_Nablsi
No dear, I donot want this decision making structure. It should automatically call the function that has been chosen by the user.
When a user select an item in the ComboBox the SelectedIndexChanged and SelectedValueChanged are fired and there you can check the selected item and call whatever function fits but to call the function immediately once an item selected I don't think there's a way to do this but to derive the ComboBox control and the ComboBoxItem, handle the SelectedIndexChanged internally and call the function assotiated with the derived ComboBoxItem. Anyways I would like to see other people opinions it is an interesting question.
A_Nablsi
A: 

Make your subs functions (the only difference is the returning of a value) and put them in their own class:

Public Class RunFunctions
    Dim oMessageBox As MessageBox
    Public Function One() As String
        'oMessageBox = MessageBox
        Return "Message One"

    End Function

    Public Function Two() As String
        Return "Message Two"

    End Function
End Class

Add Each function from the class as an item in your combo box:

Public Class Combo_Functions
    Dim oRunFunction As RunFunctions
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object _
           , ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        MessageBox.Show(ComboBox1.Items(ComboBox1.SelectedIndex()))

    End Sub

    Private Sub Combo_Functions_Load(ByVal sender As Object _
                                       , ByVal e As System.EventArgs) Handles Me.Load
        oRunFunction = New RunFunctions

        ComboBox1.Items.Add(oRunFunction.One())
        ComboBox1.Items.Add(oRunFunction.Two())

    End Sub
End Class

When the combo box is changed (or use the code for the button click) the messagebox for the correct function is executed.

Jeff O
Gives error Reference to a non-shared member requires an object reference, while I have put combobox1 in the form
Make sure you exclude the code: 'Public Class Combo_Functions' and use the code on your form.
Jeff O
A: 

It looks like what you're trying to do is to dynamically call a particular method using a string variable that contains its name. For example, the combo box would contain items "One" and "Two", and you would call the sub named "One" if the first item in the combo box is selected, or the sub named "Two" if the second item is selected. To that end, you may find this article interesting:

http://www.codeproject.com/KB/cs/CallMethodNameInString.aspx

The code in the article is in C#, which shouldn't be too difficult to convert to VB. But here's the translated version of the code for simply invoking a method without passing or returning any parameters (note: I have not tested this code). It simply uses reflection to find the appropriate method:

Public Shared Sub InvokeStringMethod(ByVal typeName As String, ByVal methodName As String)
    'Get the type of the class
    Dim calledType As Type = Type.[GetType](typeName)

    'Invoke the method itself
    calledType.InvokeMember(methodName, BindingFlags.InvokeMethod Or BindingFlags.[Public] Or BindingFlags.[Static], Nothing, Nothing, Nothing)
End Sub

You simply pass the name of the class that contains the method(s) you want to call as the typeName and the name of the method itself that you want to call as the methodName:

InvokeStringMethod("MyClass", "Two")
Cody Gray
Thanks, I tried the code it gives error BindingFlags not declared. Could you check it for me please?
You have to have the line "Imports System.Reflection" at the very top of your code file. "BindingFlags" is declared in the System.Reflection namespace, and this tells the compiler to look there for it.
Cody Gray