calling object methods on a variable with interface type 
Author Message
 calling object methods on a variable with interface type

Hello,

When I tried it out, the run-time type of the variable implemented
ToString(), and that implementation is what gets called.  The following
example prints "hello":

Imports System
Public Interface myFace
        function bar() As String
End Interface

Public Class myImpl
        Implements myFace
        Function bar As String Implements myFace.bar
                return "hello"
        End Function

        Public Overrides Function ToString() As String
                return bar()
        End Function
End Class

Module Main
        Sub Main
                Dim foo As MyFace = New myImpl()
                Console.WriteLine(Ctype(foo, Object).ToString())
        End Sub
End Module

Thanks for the reply,
Mitch

Quote:

> Hi Nick,

> Not sure that will help the original poster. If his interface does not
> contain overridden ToString() method, then Object's ToString() will be
> invoked, and it is hardly helpful.



Tue, 22 Feb 2005 03:40:08 GMT  
 calling object methods on a variable with interface type
Hello Mitchell,

Oh yes, of course I meant "his class does not contain overridden
ToString()", not "interface".
Sorry for inconvenience.

--
Dmitriy Lapshin
X-Unity Unit Testing and Integration Environment
http://x-unity.miik.com.ua


Quote:
> Hello,

> When I tried it out, the run-time type of the variable implemented
> ToString(), and that implementation is what gets called.  The following
> example prints "hello":

> Imports System
> Public Interface myFace
> function bar() As String
> End Interface

> Public Class myImpl
> Implements myFace
> Function bar As String Implements myFace.bar
> return "hello"
> End Function

> Public Overrides Function ToString() As String
> return bar()
> End Function
> End Class

> Module Main
> Sub Main
> Dim foo As MyFace = New myImpl()
> Console.WriteLine(Ctype(foo, Object).ToString())
> End Sub
> End Module

> Thanks for the reply,
> Mitch


> > Hi Nick,

> > Not sure that will help the original poster. If his interface does not
> > contain overridden ToString() method, then Object's ToString() will be
> > invoked, and it is hardly helpful.



Tue, 22 Feb 2005 15:03:27 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Object variable or With block variable not set when calling COM Dll

2. Calling a COM call back object's method fails if it is in Windows 98

3. How to call nondefault interface methods

4. Returning an Image by calling an interface method...

5. VB client, failed method calls, ISupportErrorInfo and custom interfaces

6. const interfaces/methods( was objects)

7. Enumerating objects in VB and using OLE interface methods

8. Type problem: passing variables to methods

9. VB5 Calling ActiveX method - Type Mismatch error

10. VB5 Calling ActiveX method - Type Mismatch error

11. Calling a method based on a variable

12. Type mismatch when calling COM methods

 

 
Powered by phpBB® Forum Software