473,326 Members | 2,136 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,326 software developers and data experts.

Need some help understanding abstract

I have a class that I need to adapt to various scenarios. Some
properties and methods will be needed in every case, while other's are
unique for one case. So I made a base class, and a set of other derived
classes for each scenario.

But I don't understand this fully. There are not only methods in my
original class. It has data too, in private fields.

public class OriginalClass
{
private string field1;
private string field2;

// Constructor
public OriginalClass(string field1, string field2)
{
this.field1 = field1;
this.field2 = field2;
}

public void ProcessData()
{
// Using field1 and field2 here.
}
}

Does this mean it's no use having fields in an abstract class? I wonder
if I have to write derived classes so that each method takes all the
data necessary as parameters? Like this:

public class BaseClass
{
// No private fields and no constructor

public void ProcessData(string field1, string field2)
{
// Using field1 and field2 here.
}
}

public class DerivedClass
{
private string field1;
private string field2;

public DerivedClass(string field1, string field2)
{
this.field1 = field1;
this.field2 = field2;

ProcessData(field1, field2);
}
}

Gustaf
Feb 6 '06 #1
3 1163
Sorry, I forgot abstract here:

public abstract class BaseClass
{
// No private fields and no constructor

public void ProcessData(string field1, string field2)
{
// Using field1 and field2 here.
}
}
Feb 6 '06 #2
Does this mean it's no use having fields in an abstract class?


No it doesn't. You can certainly have fields in acstract classes too.

Of course if you want derived classes to access them you should
declared them as protected or exposed them via properties.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 6 '06 #3

"Gustaf" <gu*****@algonet.se> wrote in message
news:Ov*************@TK2MSFTNGP10.phx.gbl...
I have a class that I need to adapt to various scenarios. Some properties
and methods will be needed in every case, while other's are unique for one
case. So I made a base class, and a set of other derived classes for each
scenario.

But I don't understand this fully. There are not only methods in my
original class. It has data too, in private fields.

public class OriginalClass
{
private string field1;
private string field2;

// Constructor
public OriginalClass(string field1, string field2)
{
this.field1 = field1;
this.field2 = field2;
}

public void ProcessData()
{
// Using field1 and field2 here.
}
}

Does this mean it's no use having fields in an abstract class? I wonder if
I have to write derived classes so that each method takes all the data
necessary as parameters? Like this:

public class BaseClass
{
// No private fields and no constructor

public void ProcessData(string field1, string field2)
{
// Using field1 and field2 here.
}
}

public class DerivedClass
{
private string field1;
private string field2;

public DerivedClass(string field1, string field2)
{
this.field1 = field1;
this.field2 = field2;

ProcessData(field1, field2);
}
}

Gustaf


Here is an example of a base, abstract class that contains private members:

public abstract class BaseClass
{
private string field1;
private string field2;

protected BaseClass(string field1, string field2)
{
this.field1 = field1;
this.field2 = field2;
}

// Make field1 visible to derived classes, and readonly as well.
protected string Field1
{
get {
return field1;
}
}

protected int DoSomething
{
// Do something with field2.
return field2.Length;
}
}

public class DerivedClass : BaseClass
{
public DerivedClass(string field1, string field2) : base(field1, field2)
{ }

public void ProcessData()
{
string field1 = this.Field1;
int field2Length = this.DoSomething();
}
}

:)

Mythran

Feb 6 '06 #4

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

Similar topics

15
by: Tee | last post by:
Hi, I have a base usercontrol with a method (blank method, no code), I have another few usercontrols that will inherit this base usercontrol, but I want to force all the usercontrol that...
2
by: Bonj | last post by:
Hello Can anyone assist with the following class hierarcy problem? I have a series of window classes, the object model currently being as such: Window / | \ / | \...
4
by: vidalsasoon | last post by:
In Visual Studio I use object viewer to look at a .NET wrapped dll. One class has this signature: public abstract class Slider : ODE.Joints.Joint I don't think the "Slider" class should be...
2
by: Rahul | last post by:
I am developing an application which need to support various data sources like SQL Server, Access, Oracle etc I am in search of a design pattern which will best suit my requirement. Can...
3
by: chris | last post by:
Hallo, I am in need of a replacement for the Microsoft Visual Studio .NET. The reason is quiet simple. I develop forms which are used on different microsoft windows platform, and one...
11
by: Bryan Kyle | last post by:
Hi All, I'm fairly new to C# and Generics and I'm wondering if anyone has some suggestions for me. I'm trying to implement a simple DAO framework using generics to keep my code as clean as I...
10
by: satan | last post by:
I need the definitions of the method copyQueue, the default constructor, and the copy constructor folr the class LinkedQueueClass. This is what i get so far public abstract class DataElement {...
1
by: HarishAdea | last post by:
Hi, I am trying to run the JAVA pgm, but it is giving error as "selection does not contain a main type". The filename is "ScoreLeadSummary.java" when i try to run it or debug,it gives the pop...
2
Dormilich
by: Dormilich | last post by:
Hi, I'm testing my classes for a web page and I stumble upon an error I don't have a clue what it means: Error: Fatal error: Can't use method return value in write context in "output.php" on...
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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.