B
Bill
Hi,
This problem is more difficult to explain than the ones in my previous
posts. For this one, I just need some good design practice advice,
from an intuitive point of view.
I have three classes, named Plot1, Plot2 and Plot3. Plot2 derives from
Plot1, and Plot3 derives from Plot2.
I have two more classes, named Cell2 and Cell3. Cell3 derives from
Cell2. So far, so good.
Now, one of the member fields of Plot2 is an array of cells of type
Cell2 (declaration is "public Cell2[,] cell;").
And one of the member fields of Plot3 is an array of cells of type
Cell3 (declaration is "new public Cell3[,] cell;").
Graphically:
Plot1
|
|
Plot2 has member field of type Cell2
| |
| |
Plot3 has member field of type Cell3
Suppose that the kind of plot that I need in my application is an
instance of Plot3.
I separated Plot3 from Plot2 (and Cell3 from Cell2) expecting to
reuse, in Plot3, a lot of code present in Plot2. That is, most of the
operations with cells need only the member fields present in Cell2,
and those operations are done at level 2 (in Plot2 code). I wanted to
reuse that code in level 3, and only add the new code (in Plot3) which
is specific for member fiels of Cell3 _not_ present in Cell2. However,
it looks like I can't reuse anything, because every time I invoke,
from Plot3 (using base.<whatever>()), the code in Plot2 that refers to
cells, it refers to instances of type Cell2, not Cell3. And I haven't
allocated space for the Cell2 cells. Only for the Cell3 cells, of
course. I mean, when I run my application, I don't want any "...=new
Cell2[..,..]" being executed. Only "...=new Cell3[..,..]". Those cells
take a lot of memory each, and the level2 ones would never be used
anyway.
So, how to make the code in Plot2 that does the "common" operations
with cells, refer to the most-specific cells (which, when called from
a Plot3 instance, would be Cell3 cells), instead of to its Cell2
cells?
I mean, I need something like virtual/override, but for member fields.
The compiler doesn't let me use those modifiers for member fiels.
Anyone got the idea of my problem? I think this must be a relatively
usual need. How to proceed in these cases?
Thank you very much,
Bill
This problem is more difficult to explain than the ones in my previous
posts. For this one, I just need some good design practice advice,
from an intuitive point of view.
I have three classes, named Plot1, Plot2 and Plot3. Plot2 derives from
Plot1, and Plot3 derives from Plot2.
I have two more classes, named Cell2 and Cell3. Cell3 derives from
Cell2. So far, so good.
Now, one of the member fields of Plot2 is an array of cells of type
Cell2 (declaration is "public Cell2[,] cell;").
And one of the member fields of Plot3 is an array of cells of type
Cell3 (declaration is "new public Cell3[,] cell;").
Graphically:
Plot1
|
|
Plot2 has member field of type Cell2
| |
| |
Plot3 has member field of type Cell3
Suppose that the kind of plot that I need in my application is an
instance of Plot3.
I separated Plot3 from Plot2 (and Cell3 from Cell2) expecting to
reuse, in Plot3, a lot of code present in Plot2. That is, most of the
operations with cells need only the member fields present in Cell2,
and those operations are done at level 2 (in Plot2 code). I wanted to
reuse that code in level 3, and only add the new code (in Plot3) which
is specific for member fiels of Cell3 _not_ present in Cell2. However,
it looks like I can't reuse anything, because every time I invoke,
from Plot3 (using base.<whatever>()), the code in Plot2 that refers to
cells, it refers to instances of type Cell2, not Cell3. And I haven't
allocated space for the Cell2 cells. Only for the Cell3 cells, of
course. I mean, when I run my application, I don't want any "...=new
Cell2[..,..]" being executed. Only "...=new Cell3[..,..]". Those cells
take a lot of memory each, and the level2 ones would never be used
anyway.
So, how to make the code in Plot2 that does the "common" operations
with cells, refer to the most-specific cells (which, when called from
a Plot3 instance, would be Cell3 cells), instead of to its Cell2
cells?
I mean, I need something like virtual/override, but for member fields.
The compiler doesn't let me use those modifiers for member fiels.
Anyone got the idea of my problem? I think this must be a relatively
usual need. How to proceed in these cases?
Thank you very much,
Bill