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