473,320 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Issue with moving a common method to base class

I have too similar methods in two classes:

-----------------------DistributionFoo---------------------
class DistributionFoo : FooBase
{
private SortableBindingList<DistributionRowStatusmSelected ;

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<ReportRowStatusmSelected;

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<objectmSelected;

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<DistributionRowStatusmSelected ;

}

-----------------------ReportFoo---------------------
class ReportFoo : FooBase
{
private SortableBindingList<ReportRowStatusmSelected;

}
Dec 7 '07 #1
3 1398
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<Twhere T : IBaseElement
{
protected List<TmSelected;

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[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<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" <fi********@yahoo.comwrote in message
news:2c**********************************@l16g2000 hsf.googlegroups.com...
>I have too similar methods in two classes:

-----------------------DistributionFoo---------------------
class DistributionFoo : FooBase
{
private SortableBindingList<DistributionRowStatusmSelected ;

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<ReportRowStatusmSelected;

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<objectmSelected;

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<DistributionRowStatusmSelected ;

}

-----------------------ReportFoo---------------------
class ReportFoo : FooBase
{
private SortableBindingList<ReportRowStatusmSelected;

}

Dec 7 '07 #2
Hi Amdrit,

Thanks for the advice! Are you suggesting using generics? I'll try it
out.
Dec 7 '07 #3
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,
Dec 10 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ravi Tallury | last post by:
Hi We are having issues with our application, certain portions of it stop responding while the rest of the application is fine. I am attaching the Java Core dump. If someone can let me know what...
15
by: Ladvánszky Károly | last post by:
Entering 3.4 in Python yields 3.3999999999999999. I know it is due to the fact that 3.4 can not be precisely expressed by the powers of 2. Can the float handling rules of the underlying layers be...
3
by: CaribSoft | last post by:
I want to create my own common dialog to use in an application . How do I show a custom dialog (form) in a class based on the common dialog class?
3
by: jqpdev | last post by:
Hello all, I've been developing web apps using Borland's websnap technology which is built upon asp technology. I'm tranisitioning to ASP.NET VS.NET and need some techniques/best practices to...
5
by: wrecker | last post by:
Hi all, I have a few common methods that I need to use at different points in my web application. I'm wondering where the best place would be to put these? I think that I have three options. ...
2
by: Diogo Alves - Software Developer | last post by:
Greetings I would like to knowhow can I put a sliding panel... I've done this: if (panel1.Width < 300) { while (panel1.Width < 300) { panel1.Width = panel1.Width + 40;
5
by: toton | last post by:
Hi, I want a few of my class to overload from a base class, where the base class contains common functionality. This is to avoid repetition of code, and may be reducing amount of code in binary,...
11
by: =?Utf-8?B?R29rdWw=?= | last post by:
I am struck up with a problem and want anyone here to help me out. I am a beginner in .NET trying to learng DataBinding concepts. I have binded 4 text boxes with a dataset but when I say...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.