473,397 Members | 1,960 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,397 software developers and data experts.

A simple inheritance question

Hi,

I currently have a class called DefinedTest which relates to a set of
tests I perform. But there are two Test states, those that are defined
and those that are not. I'd therefore like to have two classes
instead, Test and DefinedTest with the former storing either Defined
or non-Defined Tests and the latter only defined tests.

The distinction between the two classes in terms of members is very
simple. A defined test has a file on the local computer which exists,
the undefined test has a file which is not present on the local
computer.

I therefore wish to have something like this -

DefinedTest
{
ExistingFile file;
}

Test
{
File file;
}

ExistingFile and File are classes I have writen also. ExistingFile is
inherited from File with the only difference being that ExistingFile's
constructor and set check that the file exists locally. If not, an
exception is called. I use this class a lot in my code as it confirms
that the file exists when the object is constructed and I therefore
don't have to worry about files being missing - of course the file
could be deleted after the constructer is called, but this is not an
issue for me.

So here's my question. DefinedTest and Test are identical in terms of
members, all except for the file member. Is it possible for me to have
DefinedTest as being inherit from Test? How would you implement this?

Thanks,

Barry

Oct 25 '07 #1
3 1227
<bg***@yahoo.comschrieb im Newsbeitrag
news:11**********************@k79g2000hse.googlegr oups.com...
Hi,

I currently have a class called DefinedTest which relates to a set of
tests I perform. But there are two Test states, those that are defined
and those that are not. I'd therefore like to have two classes
instead, Test and DefinedTest with the former storing either Defined
or non-Defined Tests and the latter only defined tests.

The distinction between the two classes in terms of members is very
simple. A defined test has a file on the local computer which exists,
the undefined test has a file which is not present on the local
computer.

I therefore wish to have something like this -

DefinedTest
{
ExistingFile file;
}

Test
{
File file;
}
Given that the member-variable is set only while creation, I'd propose
following:

class Test
{
File file;

public Test (File file)
{
this.file = file;
}

.....
}

class DefinedTest
{
public DefinedTest (ExistingFile file) : base (file)

....
}
HTH

Christof

Oct 25 '07 #2
Well .... if Test can store either type of test and if ExistingFile inherits
from File why not just

Test
{
protected File file;
}

?

You could, if you wanted do this -

DefinedTest : Test
{
// And you have access to file in here
}

You maybe need to state what you're trying to achieve rather than what you
want your classes to look like.
i.e. "I want to run a test, and if the file is local I want to do this, but
if the file is not local I want this to happen ..." You may not even need
inheritance to address your problem.

HTH,

- Adam.
=========
Oct 25 '07 #3
bg***@yahoo.com wrote:
[...]
So here's my question. DefinedTest and Test are identical in terms of
members, all except for the file member. Is it possible for me to have
DefinedTest as being inherit from Test? How would you implement this?
The two replies seem to offer a good start, assuming you really want
this to be done via inheritance. But as Adam says, are you really sure
you need to be represented through a derived class?

It seems to me that having a separate property on a single class,
indicating whether the Test is Defined or not would make more sense.
The property could simply be initialized (by setting a private field)
during construction by checking for the file existence, or the
constructor could include a parameter indicating whether the file must
exist or not (and then throw an exception if the desired state doesn't
match the actual state).

That said, if you really feel it makes your code better to have the
"Defined" nature of the object represented by the type, I'd agree that
the suggestion provided already to simply have a single File member
(private field, and public property if you need access to the data
outside the class) seems fine to me. If you have some specific
reasoning behind storing the filename differently depending on which
class it's in, then perhaps you could elaborate on that.

Pete
Oct 25 '07 #4

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

Similar topics

1
by: KK | last post by:
Windows Forms Inheritance, Incomplete? I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can be used effectively if the...
2
by: KK | last post by:
** Posting it here cause after couple of days no body responded.** I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can...
6
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
1
by: relient | last post by:
Hi, I just started the chapter on "Inheritance" and I'm a bit confused so I have three questions: first, here's the code: using System; using System.Collections.Generic; using System.Text; ...
6
by: relient | last post by:
Hi, I have three classes: Animal, Dog and Cat. Dog inherits from Animal and Cat inherits from Dog. My question is: is Cat now also a Animal even though it didn't inherit directly from Animal?
3
by: RSH | last post by:
I have a simple question regarding inheritance in a web form. I have a DropDownList in an aspx form. It is called DropDownList1 I have a class that will be overriding the render event so I...
1
by: Oskar Bennet | last post by:
Hi everybody, I am supposed to draw a simple UML diagram for a very small project that consists of less than a dozen classes. I have never been working with UML before, I have read some tutorials...
8
by: Devon Null | last post by:
I was wondering if there is a container (i.e. a vector) that can hold data structures of differing sizes. I was thinking of something along the lines of a container of classes. Think of a backpack...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.