Overloaded function with ListControl parameter

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 ?
 
G

Guy

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top