Click here to Skip to main content
15,891,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I don't see a way of making this easier/shorter, right now i have the following

VB
Sub DisplayObjects(ByRef Form As Form, ByRef listBox As ListBox, ByVal FinalIndexOfListBox As Integer)


       Select Case (Form.Name)

           Case (FrmConfigureDepartments.Name) 'Departments
               EnableEvents = False 'we dont want the listbox index changed event to fire so we disable the events until we are done
               listBox.Items.Clear()

               For Each Obj In ModuleTimeClock.DAL.GetDepartments
                   listBox.Items.Add(Obj)
               Next
               EnableEvents = True 'enable events again so when we set the index to 0 it displays the objects properties
               listBox.SelectedIndex = FinalIndexOfListBox

               Case()

       End Select

   End Sub


I would like to be able to declare the function as

Sub DisplayObjects(ByRef Form As Form, ByRef listBox As ListBox, ByVal FinalIndexOfListBox As Integer,ObjectType as Type,ObjectList as List(of Objects))

And then just say

VB
For Each Obj In ObjectList
                  listBox.Items.Add(Ctype(Obj,ObjectType))
              Next


But i can;t seem to find a way of doing this, i would prefer this way cause if i have to do the previous, then i would have to repeat that code 13 more times, due to apssing in Depeartment,Users,Activities,Shifts Etc..
Any help on this would be greatly appreciated THANKS in Advance :)
Posted
Comments
Dave Kreskowiak 15-Aug-14 22:36pm    
What is this for? If it is what I think it's for, it's a pretty bad idea.

1 solution

OK, that's what I thought. Bad idea.

Look at what you're doing in your Select Case. This is writing code specific to each form. That kind of code belongs on the form it's responsible for, not in some code that aspires to be the "Be-All End-All" to populating controls on a form.

A better way to do this would be an extension method for IEnumberable(Of T). Now you're not passing any type information about the list you're sending. All you need to specify is the ListBox your populating:
Imports System.Collections.Generic
Imports System.Runtime.CompilerServices

Module Extensions

    <extension()>
    Public Sub SendToListBox(Of T)(list As IEnumerable(Of T), target As ListBox)
        If target Is Nothing Then
            Throw New ArgumentNullException("target")
        End If

        target.BeginUpdate()
        target.Items.Clear()

        For Each item In list
            target.Items.Add(item)
        Next

        target.EndUpdate()
    End Sub

End Module

Though, in your example, I have no idea what EnableEvents is so that can't be incorporated into the extension.

Now all you have to do to populate a ListBox is:
myCollection.SendToListBox(someListBox)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900