Issue with moving a common method to base class

Issue with moving a common method to base class

am 07.12.2007 21:09:32 von Fir5tSight

I have too similar methods in two classes:

-----------------------DistributionFoo---------------------
class DistributionFoo : FooBase
{
private SortableBindingList 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 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 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 mSelected;

}

-----------------------ReportFoo---------------------
class ReportFoo : FooBase
{
private SortableBindingList mSelected;

}

Re: Issue with moving a common method to base class

am 07.12.2007 22:30:29 von AMDRIT

I haven't tried this, but how about creating a common interface that each
type works against?

internal interface IBaseElement
{
void Execute();
}

internal abstract class baseclass where T : IBaseElement
{
protected List mSelected;

protected baseclass()
{
mSelected = new List();
}

protected abstract Type GetElementType();
protected abstract void ReportProgress(decimal percentComplete, int
position);
protected abstract void UpdateControls();
protected void DoSomething()
{

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

IBaseElement tempElement = (IBaseElement)mSelected[i];

tempElement.Execute();

ReportProgress(i / mSelected.Count, i);

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

internal class DistributionRowStatus : IBaseElement
{

#region IBaseElement Members

public void Execute()
{
//
}

#endregion
}

internal class ReportRowStatus : IBaseElement
{

#region IBaseElement Members

public void Execute()
{
//
}

#endregion
}

internal class Class1 : baseclass
{
public Class1():base()
{
//
}

protected override Type GetElementType()
{
return System.Type.GetType("DistributionRowStatus");
}

protected override void ReportProgress(decimal percentComplete, int
position)
{
//Do custom work here
}

protected override void UpdateControls()
{
//Do custom work here
}
}

internal class Class2 : baseclass
{
public Class2(): base()
{
//
}

protected override Type GetElementType()
{
return System.Type.GetType("ReportRowStatus");
}

protected override void ReportProgress(decimal percentComplete, int
position)
{
//Do custom work here
}

protected override void UpdateControls()
{
//Do custom work here
}
}

"Curious" wrote in message
news:2c787809-ca05-444b-831a-5e3a54a039a1@l16g2000hsf.google groups.com...
>I have too similar methods in two classes:
>
> -----------------------DistributionFoo---------------------
> class DistributionFoo : FooBase
> {
> private SortableBindingList 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 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 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 mSelected;
>
> }
>
> -----------------------ReportFoo---------------------
> class ReportFoo : FooBase
> {
> private SortableBindingList mSelected;
>
> }

Re: Issue with moving a common method to base class

am 07.12.2007 22:38:49 von Fir5tSight

Hi Amdrit,

Thanks for the advice! Are you suggesting using generics? I'll try it
out.