jQuery and ASP.NET

January 27, 2009

Multiple Inheritance in C# and VB.NET




C# and VB.NET supports single inheritance, however they do support multiple 'interface' inheritance:

Here's a sample demonstrating the same:

C#


//Single Inheritance

 

public class A

{

    public A() { }

}

 

public class B : A

 

{

    public B() { }

}

 

 

 

//Multiple Interface Inheritance

 

interface IComparable

{

    int CompareTo(object obj);

}

 

interface ISomethingElse

{

    int EqualTo();

}

 

 

 

public class Z :  IComparable, ISomethingElse

{

    public int CompareTo(object obj)

    {

        // implementation code goes here 

    }

 

public int EqualTo()

    {

        // implementation code goes here 

    }

 

}



VB.NET


'Single Inheritance

 

Public Class A

    Public Sub New()

    End Sub

End Class

 

Public Class B

    Inherits A

 

    Public Sub New()

    End Sub

End Class

 

 

 

'Multiple Interface Inheritance

 

Friend Interface IComparable

    Function CompareTo(ByVal obj As Object) As Integer

End Interface

 

Friend Interface ISomethingElse

    Function EqualTo() As Integer

End Interface

 

 

 

Public Class Z

    Implements IComparable, ISomethingElse

    Public Function CompareTo(ByVal obj As Object) _
     As Integer Implements IComparable.CompareTo

        ' implementation code goes here 

    End Function

 

    Public Function EqualTo() As Integer Implements _
       ISomethingElse.EqualTo

        ' implementation code goes here 

    End Function

 

End Class



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

2 Responses to "Multiple Inheritance in C# and VB.NET"
  1. My Dreams said...
    October 31, 2010 7:58 PM

    Hi,
    I need to create form2 with same controls as forms1( will Add new controls in Form2). Most of the methods in form1 will be used in form2, I'm wondering whether I can acheive re-using the code from form1 using Multiple Inheritance. Please help me.

  2. Suprotim Agarwal said...
    October 31, 2010 9:19 PM

    You are referring to Form Inheritance. Take a look here http://msdn.microsoft.com/en-us/library/s82eeh07(v=VS.90).aspx

 

Copyright © 2009-2011 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions