Search code examples
.netvb.net.net-3.5ilist

How To Use VB.NET IList(Of T).Max


How do I use the IList(Of T).Max function in my example below?

Dim myList as IList(Of Integer)

For x = 1 to 10
    myList.add(x)
Next

'Error: 'Max' is not a member of 'System.Collections.Generic.IList(Of Integer)'
MsgBox(myList.Max()) 

Solution

  • your code throws a System.NullReferenceException when calling myList.add because it has not been initialized. If you use List instead of IList as shown below it works.

    Imports System.Collections.Generic
    Module Module1
        Sub Main()
    
            Dim myList As New List(Of Integer)
    
            For x = 1 To 10
                myList.Add(x)
            Next
    
            MsgBox(myList.Max())
    
        End Sub
    End Module
    

    It works fine even if only System is imported in the project.