From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I pass the parent information into child class?


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.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 11 of 16
(1,680 Views)

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:

 

AllParents.PNG

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:

 

MostParentsOneChild.PNG

 

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

 

 

Message 12 of 16
(1,671 Views)

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.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 13 of 16
(1,658 Views)

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

CAUTION! New LabVIEW adopters -- it's too late for me, but you *can* save yourself. The new subscription policy for LabVIEW puts NI's hand in your wallet for the rest of your working life. Are you sure you're *that* dedicated to LabVIEW? (Summary of my reasons in this post, part of a voluminous thread of mostly complaints starting here).
0 Kudos
Message 14 of 16
(1,632 Views)

@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

0 Kudos
Message 15 of 16
(1,629 Views)

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.

Message 16 of 16
(1,619 Views)