Convert object to type of interface

Hi

I have a normal form that I have added one function into.

Public sub Input(data as string)

End sub

I receive this form as an object and try to use

Ctype(FormObj, ISomeInterface)

but it comes up with an invalid cast exception

A CType(FormObj, Form) works fine but I cant access the Input function


Thanks
Adam
LVL 4
adam_pedleyAsked:
Who is Participating?
 
arif_eqbalCommented:
Great
0
 
SKumar_1981Commented:
try this
Get
            Return CType(viewstate("data"), String)
        End Get
        Set(ByVal Value As String)
            viewstate("data") = Value
        End Set
regards
skumar
0
 
arif_eqbalCommented:
Hi Adam
Does your Form Class implement the Interface ??

Ctype(FormObj, ISomeInterface)

would only work if your Form Class whose object is FormObj Implements the Interface ISomeInterface

and after
Dim obj As ISomeInterface=Ctype(FormObj, ISomeInterface)
the object obj would only be able to acces the Functions that the Interface ISomeInterface provides
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
adam_pedleyAuthor Commented:
Sorry i dont understand how that works?
0
 
adam_pedleyAuthor Commented:
arif_eqbal yes my form does implement the interface

It still says its an unable to cast object
0
 
arif_eqbalCommented:
ok and here's a simple implementation of what you want

I understand you are having a Form, so lets assume the object of this form needs to Converted to the Interface type
The Form should be declared like this

Suppose you have an Interface declared like this

Public Interface ISomeInterface
     Sub Input(data as string)
End Interface

then your form would be like this

Public Class MyForm
       Inherits System.Windows.Forms.Form  '<--Standard for a Form
       Implements ISomeInterface   '<-- Our special Form Implements this Interface

      '----Usual Form Data

     Public Sub Input(Data As String) Implements ISomeInterface.Input
         '--- Your implementation of the Function
    End Sub

End Class

Then you can use the object of this class liek this
Say the object is FormObj

Dim obj As ISomeInterface=Ctype(FormObj, ISomeInterface)
obj.Input("Some Data")

Remember when you cast the FromObj to the Interface type you'd only be able to access methods that are part of the Interface, that means if your class had one more function say MyInput() the object of ISomeInterface woulod not be able to access that.
0
 
adam_pedleyAuthor Commented:
Thats exactly what i have

But it still doesnt work

I can succesfully convert it to type form but not ISomeInterface
0
 
arif_eqbalCommented:
ok...can you just post the full cast line...
are you assigning to the Interface object or to another object that implements the same interface
0
 
adam_pedleyAuthor Commented:
       Dim myScreen as IScreen
        Dim a As [Assembly] = Assembly.LoadFrom(Application.StartupPath & "\" & Location)

        Try

            Dim t As Type = a.GetType(Reference & "." & Name)
            Dim ScreenObject As Object
            ScreenObject = Activator.CreateInstance(t)
            myIScreen = CType(ScreenObject, ScreenForm)
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace)
        End Try


and the interface is

Public Interface IScreen
    Sub Input(ByVal data As String)
End Interface


And the form in another DLL
Public Class Screen1
    Implements IScreen

    Public Sub Input(ByVal data As String) Implements ScreenForm.Input
        MsgBox(data)
    End Sub
End Class
0
 
adam_pedleyAuthor Commented:
when i copied the code to here i made atypo

myIScreen = CType(ScreenObject, ScreenForm)

Should be

myScreen = CType(ScreenObject, IScreen)
0
 
adam_pedleyAuthor Commented:
Yep ok i've narrowed down the problem

Since the form is in another DLL and the interface is specified in the DLL as well as the program

even though they are coded word for word it doesnt see them as the same.
0
 
arif_eqbalCommented:
-->  myScreen = CType(ScreenObject, ScreenForm)
ScreenForm ?? Your Interface name is IScreen isn't it
Is ScreenForm another Interface or is it the name of the Form ?

I hope it should be
myScreen = CType(ScreenObject, IScreen)



0
 
arif_eqbalCommented:
ok I didn't see your post
Now when you say IScreen in
myScreen = CType(ScreenObject, IScreen)
does it show compilation error for IScreen if yes you have not referenced the Interface in this App
0
 
adam_pedleyAuthor Commented:
Yep i solved the problem.

The interface obviously needed to have the same namespace as well

So what i did

Was create a DLL and put the interface definition in there.

I then referenced the DLL from both places and used the interface in the DLL

It now converts successfully.


0
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.