Issue with moving a common method to base class

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I have too similar methods in two classes:

-----------------------DistributionFoo---------------------
class DistributionFoo : FooBase
{
private SortableBindingList<DistributionRowStatus> mSelected;

private void DoSomething()
{

for (int i = 0; i < mSelected.Count; i++)
{

DistributionRowStatus lInstance = mSelected[i];

lInstance.Execute();

ReportProgress(i / mSelected.Count, i);

if (mSelected.Count > 1)
{
UpdateControls();
}
}
}
}

-----------------------ReportFoo---------------------
class ReportFoo : FooBase
{
private SortableBindingList<ReportRowStatus> mSelected;

private void DoSomething()
{

for (int i = 0; i < mSelected.Count; i++)
{

ReportRowStatus lInstance = mSelected[i];

lInstance.Execute();

ReportProgress(i / mSelected.Count, i);

if (mSelected.Count > 1)
{
UpdateControls();
}
}
}
}

Since the methods, "DoSomething", in both classes, are identical
except for the lists that they operate on. mSelected in
"DistributionFoo" is a collection of "DistributionRowStatus" type of
objects, while it is a collection of "ReportRowStatus" type of ojects
in "ReportFoo".

I want to move "DoSomething" method to the base class from child
classes so that both "DistributionFoo" and "ReportFoo" can share it.
However, I don't know how to handle "mSelected".

I have code like below. I'm sure this doesn't work. Would welcome any
advice on how to make it work!

-----------------------FooBase---------------------
class FooBase
{
protected SortableBindingList<object> mSelected;

protected void DoSomething()
{

for (int i = 0; i < mSelected.Count; i++)
{

object lInstance = mSelected[i];

lInstance.Execute();

ReportProgress(i / mSelected.Count, i);

if (mSelected.Count > 1)
{
UpdateControls();
}
}
}
}


-----------------------DistributionFoo---------------------
class DistributionFoo : FooBase
{
private SortableBindingList<DistributionRowStatus> mSelected;

}

-----------------------ReportFoo---------------------
class ReportFoo : FooBase
{
private SortableBindingList<ReportRowStatus> mSelected;

}
.



Relevant Pages

  • Re: C++ design question
    ... "Simon Elliott" wrote: ... > to see if anyone can recommend a design improvement. ... > fooBase has a reference to an object in fooDerived. ...
    (comp.object)
  • C++ design question
    ... to see if anyone can recommend a design improvement. ... fooBase has a reference to an object in fooDerived. ... int main ...
    (comp.object)
  • Re: C++ design question
    ... the base class depends on an object in a derived class: ... > fooBase has a reference to an object in fooDerived. ...
    (comp.object)