S
S
substance2015-01-09 14:46:15
Programming
substance, 2015-01-09 14:46:15

How to cast an object to a higher type in the inheritance tree?

If I have a FileMember class inherited from Grid, I can cast it to Grid without any problems, but I need it the other way around, there is a Grid and I need to cast it to FileMember. The latter does not have a constructor.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Nemiro, 2015-01-09
@substance

You will have to make a constructor in FileMember that will take a Grid and build itself based on it. In this case, the connection with the original object will be lost (if critical).
Alternatively, you can remove the inheritance and make FileMember a property that accepts Grid . It will be easier to pass a Grid to a FileMember :) But FileMember will have to be tweaked a lot to make it behave like a Grid . In fact, there will be a stupid redirect (but pitfalls cannot be ruled out, everything can be more complicated).
In the end, you need to choose what will be easier: copy the original Grid to FileMember; or define all properties, methods of Grid , etc. in FileMember ; or abandon the idea of ​​passing Grid , use only FileMember , change the original task to a variant with a simpler solution.

G
GavriKos, 2015-01-09
@GavriKos

If your Grid was instantiated as a FileMember, then use a simple type cast (although this most likely indicates a design error). If the Grid was instantiated as a Grid, then you can't do anything.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question