Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7780177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:49:38+00:00 2026-06-01T18:49:38+00:00

I have a simple subroutine that loads a list from a database. I would

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:49:39+00:00Added an answer on June 1, 2026 at 6:49 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple HTML code with some JavaScript. It looks like: <html> <head> <script
I have simple code that uses AudioUnit to render sine wave to the output.
I have simple win service, that executes few tasks periodically. How should I pass
I have simple SSIS package which reads data from flat file and insert into
I have simple SSIS package where I import data from flat file into SQL
I have simple WinForms application where modifying Windows Registry. The problem is that in
I have simple code: <?php error_reporting(E_ALL); ini_set('display_errors', true); ini_set('html_errors', false); $test = array(); $test
I've got a simple perl subroutine that checks to see if google is still
I want to call a C subroutine from Java. I'm using JNI. I have
I know that this is probably a simple fix, but I have not been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.