473,769 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1267
<bg***@yahoo.co mschrieb im Newsbeitrag
news:11******** **************@ k79g2000hse.goo glegroups.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
3751
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 application contains couople of forms which have a consistant look and also shares SOME similar functionality between the forms.
2
2203
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 be used effectively if the application contains couople of forms which have a consistant look and also shares SOME similar functionality between the forms.
6
2100
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 so, cannot be called a WebForm itself, the child pages will implement forms). I created a Master.aspx page and removed all HTML from it, added some code to the .aspx.vb file to add controls to my page. Then I created a Child.aspx and changed the...
12
2744
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 it defines the language more completely. Great to use.
1
1781
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; namespace ConsoleProject1 { public class Base
6
1263
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
1841
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 have a snippet of this class: Public Class CustomDDL Inherits DropDownList
1
2461
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 but I cannot find the connection between UML and my C++ code. What I have found out so far is that inheritance is indicated by an arrow like this |\
8
1281
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 you can take items from and put items into of different sizes. The reason I want to know is I am about to try my hands at learning classes and need to do it in a way I can grasp - ala gaming concepts (see inventory example above.) Thanks in...
0
9586
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10210
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10043
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8869
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.