Mega Code Archive

 
Categories / VB.Net Tutorial / Generics
 

Generic method maximum returns the largest of three objects

Module MaximumTest    Sub Main()       Console.WriteLine(5, Maximum(3, 4, 5))       Console.WriteLine(Maximum(6.6, 8.8, 7.7))       Console.WriteLine(Maximum("pear", "apple", "orange"))    End Sub        Public Function Maximum(Of T As IComparable(Of T))(ByVal x As T, ByVal y As T, ByVal z As T) As T       Dim max As T = x       If y.CompareTo(max) > 0 Then          max = y       End If       If z.CompareTo(max) > 0 Then          max = z       End If       Return max    End Function End Module 5 8.8 pear