C
craigclister
Hi guys.
I am trying to make a generic function that allows me to search a
ComboBox for a specific item. As it can be used on different combo
boxes, which have items defined as different onject types, I am trying
this:
private int FindItem<Object>(ComboBox cmb, int IdToFind)
{
for (int i = 0; i <= cmb.Items.Count; i++)
{
if (cmb.Items is Object)
{
if ((cmb.Items as Object).Id == IdToFind)
{
return i;
}
}
}
return -1;
}
Then to use this function, I would use this:
cbManufacturer.SelectedIndex = FindItem<OManufacturer>(cbManufacturer,
_product.ManufacturerId);
cbManufacturer is a ComboBox. Each item is of type OManufacturer. I
have another combon on the same form called cbMeasurements which is
made up of objects of type OMeasurements. Both classes have a key as
Id. So I am trying to go through each item in the ComboBox, and mach
the 'IdToFind' with the primary key ID in the object.
It's failing though... Because:
The type parameter 'Object' cannot be used with the 'as' operator
because it does not have a class type constraint nor a 'class'
constraint \frmProductEdit.cs
Could someone assist?
I am trying to make a generic function that allows me to search a
ComboBox for a specific item. As it can be used on different combo
boxes, which have items defined as different onject types, I am trying
this:
private int FindItem<Object>(ComboBox cmb, int IdToFind)
{
for (int i = 0; i <= cmb.Items.Count; i++)
{
if (cmb.Items is Object)
{
if ((cmb.Items as Object).Id == IdToFind)
{
return i;
}
}
}
return -1;
}
Then to use this function, I would use this:
cbManufacturer.SelectedIndex = FindItem<OManufacturer>(cbManufacturer,
_product.ManufacturerId);
cbManufacturer is a ComboBox. Each item is of type OManufacturer. I
have another combon on the same form called cbMeasurements which is
made up of objects of type OMeasurements. Both classes have a key as
Id. So I am trying to go through each item in the ComboBox, and mach
the 'IdToFind' with the primary key ID in the object.
It's failing though... Because:
The type parameter 'Object' cannot be used with the 'as' operator
because it does not have a class type constraint nor a 'class'
constraint \frmProductEdit.cs
Could someone assist?