J
James CC
Hi there,
I have a function, converted from VB, that is passed an object, and sets it
up. The object passed is either a ListBox or a ComboBox. The code checks to
make sure that the object passed is one of the two types, and then does some
work. I provide a cut-down version (the first code).
The problem is that, with the exception of one line (setting the ComboBox
DropDownStyle) the work done on the items is exactly the same, but I don't
know how to simplify the code by making a variable that is EITHER a ComboBox
OR a ListBox, but unspecified, with the relevant Methods and Properties
available of both. A (non-working) example is the second code.
In the code, Box.DoStuff() is things like .BeginUpdate(), .Items.Clear(),
and so on.
Is there any way to do this? Do I have to have separate code for each type
(ComboBox and ListBox), or perhaps separate functions? I suppose I could
possibly do some funky inheritance thing, creating special classes, but this
is only two functions, so it seems like overkill.
Thanks in advance, and apologies for any Newbieness
James
public void SetBoxUp(object TheListControl)
{
if ( ! ( TheListControl.GetType() == typeof(ListBox) ||
TheListControl.GetType() == typeof(ComboBox) ) )
return;
ListControl lc = (ListControl) TheListControl;
lc.DoStuff(); // do things that are accessable to a ListControl
if ( TheListControl.GetType() == typeof(ComboBox) )
{
ComboBox cb = (ComboBox) TheListControl;
cb.DropDownStyle = ComboBoxStyle.DropDownList;
cb.DoStuff(); // do things that are accessable to a ComboBox
}
else
{
ListBox lb = (ListBox) TheListControl;
lb.DoStuff(); // do things that are accessable to a ListBox
}
}
public void SetBoxUpNew(object TheListControl)
{
if ( ! ( TheListControl.GetType() == typeof(ListBox) ||
TheListControl.GetType() == typeof(ComboBox) ) )
return;
((ListControl) TheListControl).DoStuff(); // do things that are
accessable to a ListControl
if ( TheListControl.GetType() == typeof(ComboBox) )
((ComboBox) TheListControl).DropDownStyle =
ComboBoxStyle.DropDownList;
// Get the object type, and use to Class the object correctly, then
DoStuff() - this doesn't work
( (TheListControl.GetType()) TheListControl).DoStuff(); // do
things that are accessable to a ComboBox and ListBox
}
I have a function, converted from VB, that is passed an object, and sets it
up. The object passed is either a ListBox or a ComboBox. The code checks to
make sure that the object passed is one of the two types, and then does some
work. I provide a cut-down version (the first code).
The problem is that, with the exception of one line (setting the ComboBox
DropDownStyle) the work done on the items is exactly the same, but I don't
know how to simplify the code by making a variable that is EITHER a ComboBox
OR a ListBox, but unspecified, with the relevant Methods and Properties
available of both. A (non-working) example is the second code.
In the code, Box.DoStuff() is things like .BeginUpdate(), .Items.Clear(),
and so on.
Is there any way to do this? Do I have to have separate code for each type
(ComboBox and ListBox), or perhaps separate functions? I suppose I could
possibly do some funky inheritance thing, creating special classes, but this
is only two functions, so it seems like overkill.
Thanks in advance, and apologies for any Newbieness
James
public void SetBoxUp(object TheListControl)
{
if ( ! ( TheListControl.GetType() == typeof(ListBox) ||
TheListControl.GetType() == typeof(ComboBox) ) )
return;
ListControl lc = (ListControl) TheListControl;
lc.DoStuff(); // do things that are accessable to a ListControl
if ( TheListControl.GetType() == typeof(ComboBox) )
{
ComboBox cb = (ComboBox) TheListControl;
cb.DropDownStyle = ComboBoxStyle.DropDownList;
cb.DoStuff(); // do things that are accessable to a ComboBox
}
else
{
ListBox lb = (ListBox) TheListControl;
lb.DoStuff(); // do things that are accessable to a ListBox
}
}
public void SetBoxUpNew(object TheListControl)
{
if ( ! ( TheListControl.GetType() == typeof(ListBox) ||
TheListControl.GetType() == typeof(ComboBox) ) )
return;
((ListControl) TheListControl).DoStuff(); // do things that are
accessable to a ListControl
if ( TheListControl.GetType() == typeof(ComboBox) )
((ComboBox) TheListControl).DropDownStyle =
ComboBoxStyle.DropDownList;
// Get the object type, and use to Class the object correctly, then
DoStuff() - this doesn't work
( (TheListControl.GetType()) TheListControl).DoStuff(); // do
things that are accessable to a ComboBox and ListBox
}