10-06-2017 08:47 PM - edited 10-06-2017 08:47 PM
Siegmund wrote:
There is no way, to change the type of the class from Parent to Child during run-time? At least not without losing the Parents data?. In the following example the code will always execute the childs implementation of speak VI (which brings up a pop-up saying "Child!") but we will lose the Data from the Parent (boolean will be False):
The Type input of the To More Generic Class is purely used for the data type. No values will be passed from that input.
But the child has all data and methods defined by the parent. So you can call that Write method to set the Boolean with the child class.
10-07-2017 01:41 AM
Thank you both for your answers. Your effort is greatly appreciated. I still haven't gotten the answer i was looking for though.
The Type input of the To More Generic Class is purely used for the data type. No values will be passed from that input.
But the child has all data and methods defined by the parent. So you can call that Write method to set the Boolean with the child class.
I understand this concept and I have been able to code this way succesfully. Ok, just so you understand what I actually need it for. My case looked like this:
I initialized many instances of the parent class. Nevermind the technique (if I could have used a for loop), this is just an abstraction:
Later on I realised that 1 of those Instances will need to act slightly differently (Object 3). And I know I could have solved it this way:
But I was really wondering if there is a way to downcast to a Child class later in the code, not when initializing. So I wouldn't need to alter my initialization code. I suppose there isn't. So what I got from all this till now is:
If I don't initialize my code with the Class that is lowest in Hierarchy (may that be a child or even a grandchild), I will never be able to downcast to this class and keep the data.
So if i initialize the Parent, I will keep my parent data on the class wire, but I will not be able to cast to the Child (or Grandchild) and keep the parent Data.
If I initialize the Child, I will be able to read/write Parent data as well as Child Data and cast between those two, but I will not be able to cast to the Grandchild and keep data from his ancestors.
Only and ONLY if at the beggining of my Class Wire I used the Grandchild, will I be able to cast between ancestors and keep all the Data from the whole family. Is that correct?
Br
Siegmund
10-07-2017 06:51 AM
Siegmund wrote:Only and ONLY if at the beggining of my Class Wire I used the Grandchild, will I be able to cast between ancestors and keep all the Data from the whole family. Is that correct?
Yes, that is correct. Though, you could make a "down cast" VI in the parent that reads all of the data of the parent (static dispatch) and write it to the child object (dynamic dispatch). But this is starting to sound like an anti-pattern.
10-09-2017 08:48 AM
Can you define a child-class "constructor" that accepts a parent object as input and a child object as output? Then have the child can construct its own copy of the parent object as "the parent part" of itself. You might need to create a method or two in the parent to support this kind of usage.
-Kevin P
10-09-2017 08:54 AM
@Kevin_Price wrote:
Can you define a child-class "constructor" that accepts a parent object as input and a child object as output? Then have the child can construct its own copy of the parent object as "the parent part" of itself. You might need to create a method or two in the parent to support this kind of usage.
-Kevin P
Thanks, but Crossrulz already gave me the answer i was looking for. Thanks for the input though.
Siegmund
10-09-2017 10:02 AM
I realise I'm late to the conversation but I would start considering whether inheritance isn't your problem here.
Is it not possible to have Parent pass off the work to a seperate class which is chosen (dynamically) by the parent depending ont he content of the file?
By holding a "helper" class as a data member of the parent, you can switch out the method used for any given function as required. Then pass whichever data you require from the parent to it when executing and voila, you've basically implemented a better design.
Also search for "Strategy" pattern.