Call a function on other forms

ashraf_t
ashraf_t used Ask the Experts™
on
hi, i want to call a function on other form but the problem is this other form is always undifined in design time, following example will explain what i mean

there are 3 forms (Form1, Form2, Form3) each one contains a function called "GetTheValue"

in main form i can call the function by following
form1.GetTheValue
or form2.GetTheValue
or form3.GetTheValue

offcourse all these methods are accepted

but the problem is in the following method which i want to use according to some ressons

dim F as form
F=new form1

F.GetTheValue <<<<< this is the problem, it is not accepted

How i can i solve this

Thanx


Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The problem is that defining
Dim F as form

means you can only use the the methods exposed by ALL forms.

Try defining and using an interface...
Public Interface IGetValue
The problem is that defining
Dim F as form

means you can only use the the methods exposed by ALL forms.

Try defining and using an interface...
Public Interface IGetValue
   Public Function GetTheValue() as string
End Interface

then implement this interface in your forms

Public Class Form1
    Implements IGetValue

   Public Function GetTheValue() as string Implements IGetValue.GetTheValue()
...
...
End Class

Then in your calling code declare F via the interface
Dim F as IGetValue = New Form1

dim s as string = F.GetTheValue
Adam MenkesC# ASP.NET Developer
Top Expert 2010

Commented:
Create an interface.

Have each of your forms implement the interface. Examples below

Public Interface IFormTest
    Function GetTheValue() As String
End Interface

Public Class Form1
    Implements IFormTest

    Public Function GetTheValue() As String Implements IFormTest.GetTheValue
        Return "form 1"
    End Function
End Class

In your code
Dim x As String
x = Form1.GetTheValue()

Open in new window

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
This can also be done generically without an Interface via Reflection:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim F As Form
        F = New Form1
        Dim MethodName As String = "GetTheValue"
        Dim mi As System.Reflection.MethodInfo = F.GetType.GetMethod(MethodName, Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic)
        If Not IsNothing(mi) Then
            Dim val As Integer = mi.Invoke(F, Nothing)
            Debug.Print("val = " & val)
        End If
    End Sub

    Public Function GetTheValue() As Integer
        Return 411
    End Function

End Class

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial