03-22-2023 07:32 PM
I am getting errors when I cast classes to a child? Any thoughts?
03-22-2023 07:52 PM
I guess it is because you instantiate an instance of the parent class. You cannot downcast to a child class.
Reference: How To Properly Cast Object to Child Class? - Object-Oriented Programming
03-23-2023 11:11 AM
@ZYOng wrote:
I guess it is because you instantiate an instance of the parent class. You cannot downcast to a child class.
Reference: How To Properly Cast Object to Child Class? - Object-Oriented Programming
Spot on!
The use case for the transforms are typically:
You create a basic class, e.g. Instruments
You create specific children, as DAQ, DMM and so on. Which have some added methods that are specific to their group.
You create a list of instruments, which needs to be of Instruments class to handle all.
You grab an instrument by name from your list and get the Instrument ref.
You need to use one of the specific functions on your DMM, which is listed as an Instrument:
- To more specific allows you to use the function in the code.
Alternatively you bulk/bloat your base class with all methods from all subclasses to avoid the conversion and end up with lots of unnecessary methods and code stink. This also forces all to have the same connector pane if they have the same name, how illogical it might be.
Choose your poison, sometimes it's sort of impossible to do right.