01-04-2014 06:50 PM
I am working up an OOP test program and have a problem with data access.
I have the following classes defined:
General Test
General Report
Specialized Test
Specialized Report
The Specialized Test and Specialized Report are subclasses of General Test and General Report, respectively.
The General Test class contains a General Report class instance as part of it's private data.
Specialized Test writes a Specialized Report to its parent class data on initialization. The Specialized Report contains additional data items in its private data.
I have a method for Specialized Test where I first access the Report object stored in the General Test private data, then try to access the private data of the Specialized Report, that should be returned.I get a class mismatch, as the General Test accessor does not know it is returning a Specialized Report.
Should I simply typecast the returned report to the more specific class before trying to access the subclass data as shown below, even though it will already be the more specific class?
Thanks,
Josh
01-04-2014 07:53 PM
I don't see your image - maybe it just hasn't finished uploading yet - but if I'm understanding correctly, using To More Specific Class (not Type Cast) is a fine solution here. It's reasonable to use To More Specific Class to get the wire type to match the type of data that you know it will carry when the compiler cannot make that determination for you automatically.