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?

Hi

 

Before you say it: It seems like I have misunderstood the concept of OOP.

In my project, I have a Parent class (tdms reader) that has some information inside (e.g. tdms path). Depending on the version of this tdms (that is written inside tdms) I want to fire specific child vi to read it properly.

The child class will then override reading vi.

 

Inside this specific read vi I also want to have information about tdms path that is inside parent class, so I need to inherit this information from the parent. I'm having problems doing it. Please look at the png. After going to more generic class I'm still seeing child class with incorrect parent information.

oop problem.PNG

I actually thought that I have to use parent class and cast it to more specific class (chosen child class) which gave me, obvious to you, error 1448. Can you also tell me why I can't use this casting here?

 

Thanks for the info

Download All
0 Kudos
Message 1 of 16
(6,808 Views)

First the casting issue: You have to initialize the object to the child class AT THE VERY BEGINNING.  You cannot arbitrary cast a class into a child class.

 

Now for accessing parent's data: Create a Protected method in the parent to access the data.  By making it protected, only those who inherit from the parent can access it.


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 2 of 16
(6,793 Views)


Hi

 

 

Hmm, that's interesting. I've initialized all the children classes and cast parent into one of them.

There is no casting error which is cool.

 

But why after casting am I still seeing parent class that is reading parent vi instead of child.

Shouldn't it be the child class in that wire from now on?

 oop problem.PNG

Sorry for those questions. It's kinda messing in my mind.

0 Kudos
Message 3 of 16
(6,763 Views)

When you make the array of classes, they will automatically upcast to the common parent.  So when you put Child1 and Child2 into the same array, they are cast there into Parent.  Hint: look at the coercion dots.


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
0 Kudos
Message 4 of 16
(6,758 Views)

Ha 

 

You sure know a lot about the LabVIEW OOP

Can you tell me how can I choose, on which class I want to cast during the runtime?

This doesn't work either:

oop problem.PNG

0 Kudos
Message 5 of 16
(6,755 Views)

siadajpan wrote:

Can you tell me how can I choose, on which class I want to cast during the runtime?


As I said, you cannot cast at runtime.  You have to initialize with the child class.  I do this with the Get LabVIEW Class Default Value.  You could also use the Get LabVIEW Class Default Value By Name if all of your possible child classes are already in memory.  Both of those return a class type of LabVIEW Object.  But since it initializes the class you specify, you can then down cast that to the child class, but you really just need to cast to Parent and let Dynamic Dispatch handle the rest.


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 6 of 16
(6,748 Views)

Ok, I'm almost there, I feel like still missing something.

 

My application runs with parent class being initialised at the beginning, then you "specify" which child class to use by selecting different tdms file.

When the user selects different tdms file, I have to pass child class instead of parent class, right? Or cast the child class into a parent class?

Now, when I'm creating this child class, I'll loose all the information that parent gathered when he was running.

Is there a way to preserve this information, so that when inside child class I call the parent method, I'll get the correct data? So to speak the child have an information who is his daddy?

0 Kudos
Message 7 of 16
(6,725 Views)

siadajpan wrote:  My application runs with parent class being initialised at the beginning, then you "specify" which child class to use by selecting different tdms file.

I think this is where your problem is.  When you select your TDMS file is when your child is initialized.  As part of your initialization method for the child, you use the Call Parent Method to call the parent's initialization method.  Then the output can just be of type Parent (even though the class was initialized as child).


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
0 Kudos
Message 8 of 16
(6,708 Views)

@crossrulz wrote:

siadajpan wrote:  My application runs with parent class being initialised at the beginning, then you "specify" which child class to use by selecting different tdms file.

I think this is where your problem is.  When you select your TDMS file is when your child is initialized.  As part of your initialization method for the child, you use the Call Parent Method to call the parent's initialization method.  Then the output can just be of type Parent (even though the class was initialized as child).


 

Heya Crossrulz,

 

i hope I can continue this thread, since I think you already gave me my answer with your reply to the OP, but just wanted to make sure I understand it correctly.

 

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):

 

 Typecasting.PNG

SpeakVI.PNG

 

 

And this is what the FP looks like:

 

Typecasting_FP.PNG

 

I understand that I could initialize the whole code with the Child class and go on from there. But in my case i realized midway programing that I would need a child class. 

 

Any answer would be greatly appreciated.

 

Br

 

Siegmund

 

Message 9 of 16
(6,349 Views)

Hi

 

Sorry, I can't test it now, as I don't have LV installed on my pc, but I think there is a way to do that...

If you don't need the parent class later on (after the child has been initialized), you can maybe save the instance of parent class inside the child class, and in future refer to the values using parent methods.However, if you need the parent class that runs parallel to the initialized child class, the changes in parent from now on, won't be seen from child class. The child class has the copy of the parent class that has been snapshot at the specific moment.
Let me know if that helps, and if it works, I'm curious about it

Message 10 of 16
(6,336 Views)