Search code examples
vb.netlistcontrolitemscollection

how come in VB.NET a listcontrol has no Items collection but in ASP.NET it does?


I have a simple subroutine that loads a list from a database. I would like to be able to use the same code to load a ListBox and a ComboBox by defining the list type as the common abstract base class ListControl, and see no reason why I can't - except that VB.NET doesn't expose/implement/whatever the Items collection in ListControl. I note with frustration that this is not the case in ASP.NET. At moment my code is ugly because I have to check what type of list control I have passed in, in order to cast it to a type that has an Items collection. (My code may be ugly for numerous other reasons too, but it is beautiful to me). Is there a way to rewrite the code to avoid having to go through the testing and casting nonsense? (I've stripped it down somewhat so that all that remains is where the problem lies).

Sub loadList(ByVal db As SqlDatabase, ByVal strCommandText As String, lstHost As ListControl, Optional bClearList As Boolean = True, Optional bIsListBox As Boolean = True)
    If bClearList Then
        If bIsListBox Then
            CType(lstHost, ListBox).Items.Clear()
        Else
            CType(lstHost, ComboBox).Items.Clear()
        End If
    End If
    Dim dt As DataTable = db.ExecuteDataSet(db.GetSqlStringCommand(strCommandText)).Tables(0)
    For i = 0 To dt.Rows.Count - 1
        If bIsListBox Then
            CType(lstHost, ListBox).Items.Add(dt.Rows(i)(0).ToString)
        Else
            CType(lstHost, ComboBox).Items.Add(dt.Rows(i)(0).ToString)
        End If
    Next
End Sub

Solution

  • This is because in winforms a ListBox object collection is different from a ComboBox object collection. The simplest way I can think of to tidy this is to make a helper class like

    Public Class ListHelper
        Public Shared Sub Clear(ByRef lst As ListControl)
            If TypeOf lst Is ListBox Then
                CType(lst, ListBox).Items.Clear()
            Else
                CType(lst, ComboBox).Items.Clear()
            End If
        End Sub
    
        Public Shared Sub Add(ByRef lst As ListControl, ByVal itm As Object)
            If TypeOf lst Is ListBox Then
                CType(lst, ListBox).Items.Add(itm)
            Else
                CType(lst, ComboBox).Items.Add(itm)
            End If
        End Sub
    End Class
    

    Then in your code you can just do :

    Sub loadList(ByVal db As SqlDatabase, ByVal strCommandText As String, _
      ByVal lstHost As ListControl, Optional ByVal bClearList As Boolean = True)
        If bClearList Then
            ListHelper.Clear(lstHost)
        End If
        Dim dt As DataTable = _
          db.ExecuteDataSet(db.GetSqlStringCommand(strCommandText)).Tables(0)
        For i = 0 To dt.Rows.Count - 1
            ListHelper.Add(lstHost, dt.Rows(i)(0).ToString)
        Next
    End Sub
    

    EDIT :

    Another (probably better) way to do this is using extension methods (add a new module and ) :

    Imports System.Runtime.CompilerServices
    Module ListExtensions
        <Extension()> _
        Public Sub AddToItems(ByRef lc As ListControl, ByVal itm As Object)
            If TypeOf lc Is ListBox Then
                CType(lc, ListBox).Items.Add(itm)
            ElseIf TypeOf lc is ComboBox then
                CType(lc, ComboBox).Items.Add(itm)
            Else
                'handle abuse
            End If
        End Sub
    
        <Extension()> _
        Public Sub ClearItems(ByRef lc As ListControl)
            If TypeOf lc Is ListBox Then
                CType(lc, ListBox).Items.Clear()
            ElseIf TypeOf lc is ComboBox Then
                CType(lc, ComboBox).Items.Clear()
            Else
                'handle abuse
            End If
        End Sub
    End Module
    

    Which ends up being even a bit neater in your code :

    Sub loadList(ByVal db As SqlDatabase, ByVal strCommandText As String, _
      ByVal lstHost As ListControl, Optional ByVal bClearList As Boolean = True)
        If bClearList Then
            lstHost.ClearItems()
        End If
        Dim dt As DataTable = _
          db.ExecuteDataSet(db.GetSqlStringCommand(strCommandText)).Tables(0)
        For i = 0 To dt.Rows.Count - 1
            lstHost.AddToItems(dt.Rows(i)(0).ToString)
        Next
    End Sub
    

    Here I've called these ClearItems and AddToItems to avoid ambiguity with instance methods. ListControl doesn't have .Clear() or .Add() itself but for the sake of being explicit it's probably best to have a unique nomenclature for extensions.