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

inheritance conundrum


Consider the following classes A and B.

public class A
{
public enum Person
{
Peter = 0,
Paul = 1
}

public A(Person name)
{
}
}

public class B : A
{
public B() : base(Person.Peter)
{
}
}

public class C : B
{
// ????
}
- A and B are black boxes. I've no way of altering their code.
- I write the C class. I want my C class to have all the capabilities of
the B class except for the enumeration "Person.Peter". I want "Person.Paul".

Is this possible? How should I define the C class?

Thank you.


Apr 8 '06 #1
4 1122
Don't get what you want.

As you gave B an default constructor and an init value for the parent
constructor you are set. If you don't want the B to have this hard code
value, the declare C as B

public C() : base(Person.Paul)

"bill tie" <bi*****@discussions.microsoft.com> wrote in message
news:5F**********************************@microsof t.com...

Consider the following classes A and B.

public class A
{
public enum Person
{
Peter = 0,
Paul = 1
}

public A(Person name)
{
}
}

public class B : A
{
public B() : base(Person.Peter)
{
}
}

public class C : B
{
// ????
}
- A and B are black boxes. I've no way of altering their code.
- I write the C class. I want my C class to have all the capabilities of
the B class except for the enumeration "Person.Peter". I want
"Person.Paul".

Is this possible? How should I define the C class?

Thank you.

Apr 8 '06 #2
SP

"bill tie" <bi*****@discussions.microsoft.com> wrote in message
news:5F**********************************@microsof t.com...

Consider the following classes A and B.

public class A
{
public enum Person
{
Peter = 0,
Paul = 1
}

public A(Person name)
{
}
}

public class B : A
{
public B() : base(Person.Peter)
{
}
}

public class C : B
{
// ????
}
- A and B are black boxes. I've no way of altering their code.
- I write the C class. I want my C class to have all the capabilities of
the B class except for the enumeration "Person.Peter". I want
"Person.Paul".
If the creator of A and B had wanted this behavior then they should/would
have provided a constructor that allowed you to specify this. If we use one
of the "animal" metaphors you could end up with a cat that can bark.

SP
Is this possible? How should I define the C class?

Thank you.

Apr 8 '06 #3
I presume the A contains a Person member? If there is a setter for this,
just call it from C's constructor. Something like:

public C()
{
SetPerson(Person.Paul) ;
}

If there is no method to change the Person part of A, then you are out
of luck.
Bob

"bill tie" <bi*****@discussions.microsoft.com> wrote in message
news:5F**********************************@microsof t.com...

Consider the following classes A and B.

public class A
{
public enum Person
{
Peter = 0,
Paul = 1
}

public A(Person name)
{
}
}

public class B : A
{
public B() : base(Person.Peter)
{
}
}

public class C : B
{
// ????
}
- A and B are black boxes. I've no way of altering their code.
- I write the C class. I want my C class to have all the capabilities of
the B class except for the enumeration "Person.Peter". I want
"Person.Paul".

Is this possible? How should I define the C class?

Thank you.

Apr 8 '06 #4
Well, the more important thing is what does Class B and Class A do with
Person.Peter in the constructor? If it uses it in the initialization the
class, then there's no way you can really change this.

If on the other hand, it's simply stored in a field for later use, then you
can use reflection to change the field in the base class by simply getting
the FieldInfo object for the field and setting the value. This isn't really
"recommended", but sometimes you have to do things like this. This will
require reflection permissions in the environment that the code is running,
or it will fail.

Pete Davis

"bill tie" <bi*****@discussions.microsoft.com> wrote in message
news:5F**********************************@microsof t.com...

Consider the following classes A and B.

public class A
{
public enum Person
{
Peter = 0,
Paul = 1
}

public A(Person name)
{
}
}

public class B : A
{
public B() : base(Person.Peter)
{
}
}

public class C : B
{
// ????
}
- A and B are black boxes. I've no way of altering their code.
- I write the C class. I want my C class to have all the capabilities of
the B class except for the enumeration "Person.Peter". I want
"Person.Paul".

Is this possible? How should I define the C class?

Thank you.

Apr 9 '06 #5

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

Similar topics

4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
4
by: Jim Ford | last post by:
I have a single C file with the following code: int f2() { /* Blah-blah */ } int f1() { /* Blah-blah */
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
4
by: Jim Owen | last post by:
I am storing all my application data in the application cache. Anytime I have a method as part of an asp.net form, I need to access the objects in the cache. The only way I can think of to do this...
2
by: subnunciation | last post by:
i know, this shouldnt be a conundrum right? one just shouldnt divide by zero. but this is suddenly happening *all over* my site. after chasing the error here and there, i simplified things down to:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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
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,...
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.