• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

Convert a form to a type

Hi

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?

Thanks
Adam
0
adam_pedley
Asked:
adam_pedley
  • 4
  • 4
1 Solution
 
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
0
 
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
0
 
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?...to give common functionality?

Or give us more context of what you are doing...perhaps a different approach may be in order.
0
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.

 
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.

0
 
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
        f1.Show()

        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
0
 
adam_pedleyAuthor Commented:
Genius once again idle_mind

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

VB.NET Certified Expert
Genius Level
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now