473,233 Members | 1,552 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,233 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 1160
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
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...

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.