' Visual Basic 2008 9.0 .NET Examples - Extension Methods - New in Visual Basic 2008 - 9.0 : 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 ' Visit www.studyvb.com for more examples. Copyright 2003-2008 De Wolf.