Issue with moving a common method to base class

C

Curious

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;

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;

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;

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;

}
 
A

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<T> where T : IBaseElement
{
protected List<T> mSelected;

protected baseclass()
{
mSelected = new List<T>();
}

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;

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<DistributionRowStatus>
{
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<ReportRowStatus>
{
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 said:
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;

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;

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;

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;

}
 
C

Curious

Hi Amdrit,

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

Curious

Hi Amdrit,

I don't know how your code works. Would you answer my questions below:

1) mSelected is passed to each child class. Is there a need to define
construction of baseclass in baseclass?

2) Since you've defined "GetElementType", where do you use it in the
base class?

3) What are "class1" and "class2"? Are they redundent with
"DistributionRowStatus" and "ReportRowStatus"?

Thanks,
 

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