CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2009
    Location
    .NET 2003 FWK 1.1
    Posts
    24

    [RESOLVED] Passing a Type as an argument...

    Hi,

    I'm using VB.NET 2003 with .NET Framework v1.1. I know it's getting a bit old now but I'm stuck with an existing program and don't have the time/knowhow to convert it to anything newer.

    My question would probably be solved very easily with Generics in .NET 2.0 but as I can't use that right now, I'd like to know if the following is possible:

    I have a load of manager classes which inherit from DictionaryBase and hold collections of my custom classes. For example, I have a Employee class, a Batch class, a Supplier class, etc... and each of these has a Manager classwhich holds a collection of them - eg: my PlantTrayMgr class holds collections of PlantTrays, etc..

    I have one particular form where I have to populate loads of comboboxes with the contents of various Manager objects. What I currently do in my form load event to populate comboboxes is this:
    Code:
    Option Strict On
    Option Explicit On
    
    Private _plantTrayMgr As PlantTrayMgr
    
    Private Sub frmPlantEdit_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim dictEntry As DictionaryEntry
    
            _plantTrayMgr = PlantTrayMgr.GetInstance
    
            uxTrayType.Items.Clear()        'uxTrayType is the comboBox which lists the tray types.
    
            For Each dictEntry In _plantTrayMgr             ' the enumerator of a dictionary returns a 
                                                            ' DictionaryEntry object which has to be
                                                            ' converted to the correct type before being
                                                            ' added to the combobox
    
                Dim pt As PlantTray = CType(dictEntry.Value, PlantTray)
    
                uxTrayType.Items.Add(pt)
            Next
    
    End Sub
    But I have so many comboboxes to fill I thought there must be an easier way. I wanted to add a method to my Manager classes called something like 'FillList' but to save having to type it out separately for every manager class, I was going to make a base class with this 'FillList' method in which all my manager classes could inherit from. like this:

    Code:
    Public MustInherit Class MgrBase
        Inherits System.Collections.DictionaryBase
    
        Public Sub FillList(ByVal listObject As IList, ByVal convertType As Type)
    
            Dim dictEntry As DictionaryEntry
    
            For Each dictEntry In Me.Dictionary
                listObject.Add(CType(dictEntry.Value, convertType))
            Next
        End Sub
    
    End Class
    And then I could do something like this from my forms:
    Code:
            uxTrayType.Items.Clear()
            _plantTrayMgr.FillList(uxTrayType.Items, GetType(PlantTray))
    but I get an error even before I compile the project saying "Type 'convertType' is not defined." which referrs to this line in the MgrBase class:
    Code:
                listObject.Add(CType(dictEntry.Value, convertType))
    Am I barking up the wrong tree and trying to do something which is impossible without Generics? Or do I need to override this new 'FillList' method in every single manager class in my project?

    Any ideas very much appreciated.

    John.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Passing a Type as an argument...

    Can't you BIND the combo box to the data?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Sep 2009
    Location
    .NET 2003 FWK 1.1
    Posts
    24

    Re: Passing a Type as an argument...

    Quote Originally Posted by dglienna View Post
    Can't you BIND the combo box to the data?
    I hadn't thought of that but after a bit of investigating I think my manager classes would have to implement IList but, because they inherit from DictionaryBase, they don't.

    I thought of adding the neccessary code to implement IList but because it is inherently a dictionary, it doesn't have an index as such so would be very hard to implement.

    Unless there is an easy way?

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Passing a Type as an argument...

    Convert them to tables?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Sep 2009
    Location
    .NET 2003 FWK 1.1
    Posts
    24

    Re: Passing a Type as an argument...

    I think for the amount of hassle that would take, I'm better off just having an overloaded method in each of my manager classes.

    Thanks for the suggestions though. Out of interest, am I right in thinking this kind of thing is what Generics is all about? I've read a little bit about it but not much..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured