Newbie: Copying base class

  • Thread starter Thread starter Nick
  • Start date Start date
N

Nick

Is this possible:

class A
{
protected int n = 1;
}
class B: A
{
public void m2() { n = 2;}
}
class C: A
{
public int m3() { return n;}
}

class Test
{
B b = new B();
b.m2();

C c = new C();
c = b;
// this give me an error because C has turn into B
c.m3();
}

When working with class C, I want to use/inherit all work
done on class B. However, the code above will change C to
a B, which is not what I want because I still want to use
methods in C.

Thanks.
Nick
 
Nick said:
Is this possible:

class A
{
protected int n = 1;
}
class B: A
{
public void m2() { n = 2;}
}
class C: A
{
public int m3() { return n;}
}

class Test
{
B b = new B();
b.m2();

C c = new C();
c = b;
// this give me an error because C has turn into B
c.m3();
}

No, the above would give you a compile error because an instance of B
is *never* also an instance of C. Now, what's your *actual* code which
doesn't work?
When working with class C, I want to use/inherit all work
done on class B. However, the code above will change C to
a B, which is not what I want because I still want to use
methods in C.

The code above doesn't change anything. Once you've created an object,
nothing can actually change its type.
 
Sorry. I'm still very new at this. What I'm working on
is a set of charting classes with a base chart class and
the other different types of charts inherits from this
base class (e.g. Line chart, Bar chart, Pie chart, ...etc.)

Now the problem is after I've declared a Line chart and
set all it's properties, when I declare a Bar chart, I
don't want to reset all the same properties again. I just
want to copy any properties I've set for the Line chart
and just carry on setting just the properties for the Bar
chart.

I don't want my Bar chart to inherit from my Line chart
because I don't know how it would work when I have 5 or 10
different types of charts and all sharing the same base
set of properties.

This is just a exploratory project to get me familiar with
C#. Can anyone have any suggestions? A bit of sample
code would help.

Thanks.
Nick
 
I don't have a specific answer for you, but what comes to mind, is you could
use Reflection and iterate through the properties of the first class, and
use those values to initialize the values of the second class?
 
Is there no way of doing this without having to iterate
through the properties of the first class? How about
implementing the ICloneable interface? Can I do something
with that? I can't find any documentations to use the
Clone method for my situation.

Thanks.
Nick
 
Nick said:
Is there no way of doing this without having to iterate
through the properties of the first class? How about
implementing the ICloneable interface? Can I do something
with that? I can't find any documentations to use the
Clone method for my situation.

A better solution would be to have one class for the data of the chart,
and another class for drawing the chart. That way you wouldn't need to
copy anything - just pass the same reference (to a data object) to the
pie chart as to the line chart.
 
That's a good suggestion, but I don't think it'll be as
neat as having to work with only one class instead of two
everytime a chart needs to be created.

Is there nothing in Inheritance that'll allow for this?
If class B and C both inherits from class A, is there no
way to just copy/transfer what B inherits to C? Can't we
do C = B, since they both inherits A?

Sorry for sounding like a newbie (although I am :>)
Nick
 
Nick said:
That's a good suggestion, but I don't think it'll be as
neat as having to work with only one class instead of two
everytime a chart needs to be created.

Actually, I find that many small classes usually ends up as a much
neater design than a few huge ones.
Is there nothing in Inheritance that'll allow for this?
If class B and C both inherits from class A, is there no
way to just copy/transfer what B inherits to C?

Not automatically, no. You could implement a sort of copy constructor
in A, so that passing in an instance of A (which could be an instance
of B or C) would make it copy the fields from that A into the new
object.
Can't we
do C = B, since they both inherits A?

That would only work if the variable were declared as A rather than B
or C, but it wouldn't do what you want anyway - it would just set the
variable's value to be a reference to the other object, it wouldn't
copy anything within the objects themselves.
 
Thanks.

One other alternative that I could think of is to declare
a variable of Data class within all my main classes and
have a constructor that takes in the Data class. So, if
for my Bar Chart class I could pass in the data class from
my Line Chart class. Something like:

Class a
{
ChartData x;
public a() { x = new ChartData();}
public a(ChartData cdata) {x = cdata;}
public ChartData Data { get {return x;}}
}

Class b
{
ChartData x;
public b() { x = new ChartData();}
public b(ChartData cdata) {x = cdata;}
public ChartData Data { get {return x;}}
}

LineChart a = new LineChart();
// work with a //
BarChart b = new BarChart(a.Data);
// work with b //

Can you see any problems with this?

Thanks.
Nick
 
Sorry. I'm still very new at this. What I'm working on
is a set of charting classes with a base chart class and
the other different types of charts inherits from this
base class (e.g. Line chart, Bar chart, Pie chart, ...etc.)

Now the problem is after I've declared a Line chart and
set all it's properties, when I declare a Bar chart, I
don't want to reset all the same properties again. I just
want to copy any properties I've set for the Line chart
and just carry on setting just the properties for the Bar
chart.

I don't want my Bar chart to inherit from my Line chart
because I don't know how it would work when I have 5 or 10
different types of charts and all sharing the same base
set of properties.

This is just a exploratory project to get me familiar with
C#. Can anyone have any suggestions? A bit of sample
code would help.

Thanks.
Nick

I haven't thought this through completely at this time. But, the Memento
pattern comes to mind. Or rather a bastardized version of the Memento
pattern. Ideally, the Memento pattern would only allow the original
object that set it to use it. But, it would be very easy to apply it so
you could "share" the memento with any type that would adhere to an
interface.
 
Back
Top