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

  • Home
  • SEARCH
  • 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 9054919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:48:28+00:00 2026-06-16T13:48:28+00:00

This might be impossible or a bit wrong headed, however in WinForms I’ve got

  • 0

This might be impossible or a bit wrong headed, however in WinForms I’ve got combo boxes that need populating with specific options. The project uses about 10 different forms, all with similar but slightly different functionality: hence why I didn’t use just one form and hide/show controls as appropriate.

Now I made a simple dictionary of options and fed the values with an Enum. Now I realise I’ve got duplicate code and would like to consolidate it. The option sets are date order and name order, but I’ve got one or two more to list.

This is what I’ve tried but cannot pass the dictionary into:

    Public Sub BuildOrderOptions(nameorder As ComboBox, Options As Dictionary(Of String, [Enum]))

    nameorder.Items.Clear()

    For Each item In Options
        nameorder.Items.Add(item)
    Next
    nameorder.DisplayMember = "Key"
    nameorder.ValueMember = "Value"
End Sub

Property NameOrderOptions As New Dictionary(Of String, PersonList.orderOption) From {{"First Name", PersonList.orderOption.FirstName},
                                                                                   {"Last Name", PersonList.orderOption.LastName},
                                                                                   {"Room Number", PersonList.orderOption.RoomNumber}}

Property DateOrderOptions As New Dictionary(Of String, OrderDate) From {{"Newest First", OrderDate.NewestFirst}, {"Oldest First", OrderDate.OldestFirst}}

I’ve tried a few variations with Type and [enum].getnames etc but I can’t pass the differing dictionary types in at all – I think I’ve overcomplicated the whole business by now but feel I’m missing an elegant solution. Shortly I’ll either convert back to string matching alone, or just have functions per box type – evil duplication but I can move on.

Am I right in thinking there is a nicer way to do this? Unless I just define some kind of global resource for the options maybe-but globals are bad right?

Edit: Fixed thanks to Steven. In case anyone finds it useful OR better yet, anyone can critique and make nicer, here’s the module code that all the forms can use to generate their options.

Public Sub BuildOrderOptions(nameorder As ComboBox, Options As IDictionary)
    nameorder.Items.Clear()
    For Each item In Options
        nameorder.Items.Add(item)
    Next
    nameorder.DisplayMember = "Key"
    nameorder.ValueMember = "Value"
End Sub

Property NameOrderOptions As New Dictionary(Of String, orderOption) From {{"First Name", orderOption.FirstName},
                                                                                   {"Last Name", orderOption.LastName},
                                                                                   {"Room Number", orderOption.RoomNumber}}

Property DateOrderOptions As New Dictionary(Of String, OrderDate) From {{"Newest First", OrderDate.NewestFirst}, {"Oldest First", OrderDate.OldestFirst}}

Property personStatusOptions As New Dictionary(Of String, personStatus) From {{"Active", personStatus.Active},
                                                                                  {"InActive", personStatus.InActive},
                                                                                  {"All", personStatus.All}}

Public Sub BuildComboBoxes(ListBoxes As Dictionary(Of ComboBox, IDictionary))
    For Each pair In ListBoxes
        BuildOrderOptions(pair.Key, pair.Value)
    Next
End Sub

Public Enum OrderDate
    OldestFirst
    NewestFirst
End Enum

Public Enum personStatus
    Active
    InActive
    All
End Enum

Public Enum orderOption
    None
    FirstName
    LastName
    RoomNumber
End Enum

And here’s the way I’ve got one form using it – yes, I could have had a bunch of parameters or multiple function calls: I just like having a single object giving me a single parameter to pass on.

   BuildComboBoxes( New Dictionary ( Of ComboBox , IDictionary ) From {{NameOrder, NameOrderOptions},
                                                                   {DateOrder, DateOrderOptions},
                                                                   {personStatus, PersonStatusOptions}})
  • 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-16T13:48:34+00:00Added an answer on June 16, 2026 at 1:48 pm

    You just need to change your method to accept any IDictionary object rather than a specific type of dictionary:

    Public Sub BuildOrderOptions(nameorder As ComboBox, Options As IDictionary)
    

    When you are using generics, such as Dictionary(Of TKey, TValue), the generic type is not really a type at all. You can think of it like a template for any number of specific types. So each time you use a generic type using different type parameters, they are entirely different and incompatible types. For instance:

    ' This works fine because both d1 and d2 are exactly the same type
    Dim d1 As New Dictionary(Of String, String)()
    Dim d2 As Dictionary(Of String, String) = d1
    
    ' This will not compile because d1 and d2 are completely different types
    Dim d1 As New Dictionary(Of String, Integer)()
    Dim d2 As Dictionary(Of String, Boolean) = d1
    

    As you have found out, even if you try to use a base class as the generic type parameter, the two are still incompatible. So, even though Stream is the base class for MemoryStream, you still cannot do this:

    ' This will not compile because d1 and d2 are completely different types
    Dim d1 As New Dictionary(Of String, MemoryStream)()
    Dim d2 As Dictionary(Of String, Stream) = d1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I rather expect that this might be impossible - but I was wonderring if
This might be simple or impossible ;-) Is there a way to restrict the
This might be a naive question. However, I am just trying to understand why
This might have to be broken down a bit but what i want to
This might be impossible to answer since there are probably too many variables here,
This might just be an impossible question, but I want to give it a
This might be either impossible or so obvious I keep passing over it. I
I might be searching for the wrong keywords as I find it almost impossible
OK, I'm guessing that this might be a bug but I'm not totally sure.
Since it's so hard to find an answer, I'm thinking this might be impossible.

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.