Convert a form to a type


I have a form which implements an interface.

I then get this form via an object with its type lost.

So I want to convert it back to a form that implements that interface.

How do I do that?

Who is Participating?
Mike TomlinsonMiddle School Assistant TeacherCommented:
Can't you just use two variables?

Public Interface ISomeInterface
    Function Hello() As String
End Interface

Public Class Form1
    Implements ISomeInterface

    Public Function Hello() As String Implements ISomeInterface.Hello
        Return "Hello World!"
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim f1 As New Form1

        Dim o As Object = f1 ' object returned from DLL

        ' two different variables to suit your needs?...
        Dim frm As Form = CType(o, Form)
        Dim isi As ISomeInterface = CType(o, ISomeInterface)

        ' here we use both variables
        frm.Text = isi.Hello()
    End Sub

End Class
Mike TomlinsonMiddle School Assistant TeacherCommented:
This seems to easy...what am I missing?

    If TypeOf someObject Is yourInterfaceName Then
        Dim someVar As yourInterfaceName = CType(someObject, yourInterfaceName)
        ' do something with someVar...
    End If
adam_pedleyAuthor Commented:
Public Class SomeForm
         inherits windows.forms.form
         implements ISomeInterface

End Class

I cant set the object as a the type of interface as then i lose all the form functions etc
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Mike TomlinsonMiddle School Assistant TeacherCommented:
Some thoughts...

(1) pass it as Object...then convert to Form or ISomeInterface as necessary
(2) include what you need from the Form as part of the Interface...isn't that what they are for? give common functionality?

Or give us more context of what you are doing...perhaps a different approach may be in order.
adam_pedleyAuthor Commented:
Ok i have a number of forms that each have the same set of specific functions

So each form implements the ISomeInterface.

I then use reflection to get these forms as they are not in the current application but a DLL.

Then if I CType(someForm, Form)

I lose all the functions that are implemented via the interface.

If i CType(someForm, ISomeInterface) I lose all of the form functionality such as .invoke .show etc

So i need to convert it to a type that includes the function list from both or need to declare the forms in the other DLL differently first. But im not sure how to do this.

adam_pedleyAuthor Commented:
Genius once again idle_mind

Mike TomlinsonMiddle School Assistant TeacherCommented:
Lol...I wouldn't say that was Genius...sometimes you just need an external viewpoint to see things differently.  =)
adam_pedleyAuthor Commented:
I believe your ranking speaks for itself :)

VB.NET Certified Expert
Genius Level
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.