' Visual Basic 2008 9.0 .NET Examples - Extension Methods - New in Visual Basic 2008 - 9.0 : Public Module PersonExtension _ Public Sub Print(ByVal aPerson As Person) If aPerson IsNot Nothing Then Console.WriteLine("Istance of Person : " & aPerson.ToString()) Else Console.WriteLine("No instance of Person.") End If End Sub End Module Public Class Person Private m_Name As String Public Property Name() As String Get Name = m_Name End Get Set(ByVal value As String) m_Name = value End Set End Property Public Overrides Function ToString() As String ToString = Name End Function End Class Public Class Student : Inherits Person Private m_ClassGroup As String Public Property ClassGroup() As String Get ClassGroup = m_ClassGroup End Get Set(ByVal value As String) m_ClassGroup = value End Set End Property Public Overrides Function ToString() As String ToString = Name & " (" & ClassGroup & ")" End Function End Class Public Class Example1 Public Shared Sub Main() Dim person1 As Person = New Person With {.Name = "John"} Dim person2 As Person = New Student _ With {.Name = "Jane", _ .ClassGroup = "Visual Basic .NET"} Dim person3 As Person ' person1.Print() ' (1) person2.Print() person3.Print() ' Console.ReadLine() End Sub End Class Public Class Counter Private m_Value As Integer Public ReadOnly Property Value() As Integer Get Value = m_Value End Get End Property Public Sub Raise() m_Value += 1 End Sub Public Sub Lower() m_Value -= 1 End Sub Public Overrides Function ToString() As String ToString = "Counter.Value : " & Value.ToString() End Function End Class Public Module CounterExtension _ Public Function ToInt32(ByVal aCounter As Counter) As Integer ToInt32 = aCounter.Value End Function _ Public Function ToString(ByVal aCounter As Counter) As String ToString = aCounter.Value.ToString() End Function _ Public Function ToString(ByVal aCounter As Counter, _ ByVal paddingLength As Integer) As String ' (1) ToString = aCounter.Value.ToString().PadLeft(paddingLength) End Function End Module Public Class Example2 Public Shared Sub Main() Dim counter1 As New Counter counter1.Raise() counter1.Raise() ' Dim integerValue1 As Integer = counter1.ToInt32() Console.WriteLine(integerValue1) ' Dim stringValue1 As String = counter1.ToString() ' (2) Console.WriteLine(stringValue1) ' Dim stringValue2 As String = counter1.ToString(3) ' (3) Console.WriteLine(stringValue2) ' Console.ReadLine() End Sub End Class Public Interface SomeInterface Sub SomeFirstMethod() End Interface Public Module SomeInterfaceExtension _ Public Sub SomeSecondMethod(ByVal aSomeInterface As SomeInterface) Console.WriteLine("SomeInterface.SomeSecondMethod() implementation.") End Sub End Module Public Class SomeClass : Implements SomeInterface Public Sub SomeFirstMethod() Implements SomeInterface.SomeFirstMethod Console.WriteLine("SomeClass.SomeFirstMethod() implementation.") End Sub End Class Public Class Example3 Public Shared Sub Main() Dim object1 As New SomeClass object1.SomeFirstMethod() object1.SomeSecondMethod() ' Console.ReadLine() End Sub End Class Public Interface Interface1 End Interface Public Interface Interface2 End Interface Public Module InterfaceExtensions _ Public Sub Method1(ByVal aInterface1 As Interface1) Console.WriteLine("Interface1.Method1") End Sub _ Public Sub Method2(ByVal aInterface2 As Interface2) Console.WriteLine("Interface2.Method2") End Sub End Module Public Class Class1 : Implements Interface1, Interface2 End Class Public Class Example4 Public Shared Sub Main() Dim object1 As New Class1 object1.Method1() object1.Method2() ' Console.ReadLine() End Sub End Class Namespace Administration Public Class Person Private m_Name As String Public Property Name() As String Get Name = m_Name End Get Set(ByVal value As String) m_Name = value End Set End Property Public Overrides Function ToString() As String ToString = Name End Function End Class End Namespace Namespace Administration Namespace ConsoleExtensions Public Module PersonExtension _ Public Sub Print(ByVal aPerson As Person) System.Console.WriteLine(aPerson.ToString()) End Sub End Module End Namespace End Namespace Imports Administration.ConsoleExtensions ' or on project-level (1) Public Class Application Public Shared Sub Main() Dim person1 As New Administration.Person With {.Name = "John"} 'person1.Print() ' (1) ' Console.ReadLine() End Sub End Class ' Visit www.studyvb.com for more examples. Copyright 2003-2008 De Wolf.