CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Grant Killian's Blog

No, this has nothing to do with beer -- but maybe it should?

Strange Interface Behaviour in VB.Net vs. C#

Most of my recent examples have been in VB.Net to benefit the students in my class, but I program more often in C#.  Take the following VB.Net code:

Shared Sub Main()

    dim obj as ILoggable = new PersonObject()

    MessageBox.Show( obj.ToString() )

End Sub

Public Interface ILoggable

    Sub LogState()

End Interface

Public Class PersonObject

    Implements ILoggable

    Public Sub LogState() Implements ILoggable.LogState

        'log Person state to the Person table, text file, whatever

    End Sub

End Class

 

This doesn’t compile in VB.Net because of the obj.ToString() method call. 

 

Now consider the following C# code:

[STAThread]

static void Main()

{

      ILoggable obj = new personObject();

      MessageBox.Show( obj.ToString() );

}

public class personObject: ILoggable

{

      public void LogState()

      {

            //nothing

      }

}

public interface ILoggable

{

      void LogState();

}

 

Fasten your seat belts, the C# compiles and runs; it displays “personObject” in the dialogbox when MessageBox.Show( obj.ToString() ) is called.  C# Interfaces behave as if they derive from System.Object, while VB.Net interfaces do not.  Where is the interoperability?  Yikes.  This makes Interfaces in C# more powerful since you can interrogate via Reflection. Try:

MessageBox.Show( obj.GetType().ToString() );

 

And you’ll see what I’m getting at (it displays the type name personObject).

 

Score 1 (maybe 2, if you’ve created an elaborate reflection-dependent design) for C#.  Anyone have any insight into why this is?



Comments

Grant said:

As far as "digging deeper" there are several good books on the subject. Online, I know this is a sample chapter that's not bad:
http://www.vbip.com/books/1861004974/chapter_4974_01.asp

This author has a number or articles on O'Reilly's website:
http://www.ondotnet.com/pub/a/dotnet/2002/09/22/vb-oop.html


# August 28, 2003 6:34 AM

Grant Killian said:

Some VB Static
# March 2, 2004 8:20 AM

ace said:

'interface and class
Public Interface ILoggable
Sub LogState()
Shadows Function ToString() As String
End Interface

Public Class PersonObject
Implements ILoggable

Public Sub LogState() Implements ILoggable.LogState
End Sub

Public Shadows Function ToString() As String Implements ILoggable.ToString
Return MyBase.ToString
End Function
End Class



'calling code
Dim obj As ILoggable = New PersonObject
MessageBox.Show(obj.ToString)
# November 24, 2004 8:24 AM

vandana said:

can any body tell me interfaces are refference type or value type?????
vandana_14_in@yahoo.com
# December 8, 2004 9:11 PM

sunny said:

Interfaces are reference type - vandana
# December 21, 2004 12:14 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!