Overloaded function with ListControl parameter

  • Thread starter Thread starter Vit
  • Start date Start date
V

Vit

Hi.
I'd like to pass as a parameter of class method reference
to ListControl (ListBox or ComboBox).
Here is my call:
public DoSomething(ListControl lc){}
It's ok with that, but lc does not have methods like
lc.BeginUpdate;
lc.EndUpdate
but ListBox and ComboBox does have such a methods.
I don't like two methods like
public DoSomething(ListBox lc){};
public DoSomething(ComboBox lc){};
Is there a way to do that in one method and have abilities
to use .BeginUpdate and .EndUpdate ?
 
Construct the method as you have, accepting a ListControl
as the parameter. Like so....

public DoSomething(ListControl lc)
{
//now check the param for its type
if(lc is ListBox)
{
//create a listbox from the param and use it
ListBox lb = (ListBox)lc;
//TODO do stuff with listbox..
}
if(lc is ComboBox)
{
//create a combo from the param and use it...
ComboBox cb = (ComboBox)lc;
//TODO do stuff with combo...
}

What you are doing is checking the parameter
for the object type you are looking for by using
the 'is' keyword and then casting your param
over to that type in a new variable specifically
of that type.

Hope this helps

Guy
 
Back
Top