Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use undefined Dictionary<Type, Type> as an input to method

Tags:

c#

dictionary

I am trying to define a generic method for populating ComboBox controls on Windows Forms. I am usually using Dictionary to populate my combo boxes by binding them to the Data Source. Since I have a handful of those, I wanted to make a method that I can just call on any combination of a key, value pairs. Here's my attempt:

private void PopulateDropdown(ComboBox control, Dictionary<Type, Type> dict)
{
    if (dict.Count > 0)
    {
        control.DataSource = new BindingSource(dict, null);
        control.DisplayMember = "Key";
        control.ValueMember = "Value";
    }
}

I would like to be able to call it like so:

PopulateDropdown(cbPrinters, this.inputData.Printers); where this.inputData.Printers is a Dictionary with key/value types as string, string but I also have other combinations like string, int or string,object.

I am getting an error that cannot convert string, string to Type, Type. How can I fix this?

like image 417
konrad Avatar asked Oct 22 '25 17:10

konrad


1 Answers

You need to declare your method with generic parameters:

private void PopulateDropdown<TKey,TValue>(ComboBox control, Dictionary<TKey, TValue> dict)
{
}
like image 73
René Vogt Avatar answered Oct 25 '25 06:10

René Vogt