LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Class private data is Lost after typecast to more specific class

Solved!
Go to solution

To simplifying my situation in hope to get a confirmation for this is expected behavior for class inheritance.

I have class A that has a couple private data, I have class B that inherits class A. The software performances following operation:

1. Initialize class A private data

2. Typecast to class B with "To More Specific Class function"

3. It seems like private data from step 1 is reset to default value after performing step 2. Is this expected outcome? I was expecting the private data to not change after to More Specific Class function class.

0 Kudos
Message 1 of 6
(2,733 Views)

You are getting an error when you use the To More Specific Class.  You cannot promise that A is actually B.  But if you initialized as class B, you would be fine.


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

No error message since class B inherits from class A. In this case class A is part of class B. I was expecting functions takes new implementation and private data retains original value after to more specific function call.

0 Kudos
Message 3 of 6
(2,715 Views)

To more specific class changes the data type of the control, documentation is somewhat ambiguous on what happens to the data, but I am guessing a new control is created (with default data). Maybe not what you are wanting to do, but why not just have a constant of the type B class and bundle the A class data into it?

0 Kudos
Message 4 of 6
(2,697 Views)
Solution
Accepted by JJZhang

@JJZhang wrote:

No error message since class B inherits from class A. In this case class A is part of class B. I was expecting functions takes new implementation and private data retains original value after to more specific function call.


Are you even checking for the error?  And you cannot just take A and then say it is B.  A square (B) is a rectangle (A), but a rectangle is not necessarily a square.  So unless your initially stated at creation of the object that it is B, you cannot use a To More Generic Class to cast it to B.

 

Perhaps you should post an example so we can figure out exactly what you are doing.


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 5 of 6
(2,694 Views)

A general error handling function automatically took care of it. Just wrote a dump down version and it did indeed provide an error. Thank you.

 

I actually was thinking a 4 sides shape (A) can be a square (B), or a rectangle (C). I'm loading 4 sides length and object identification from a database. Then typecast to B or C, or stay as A based on object identification from the database. But I guess you are right, it usually is going to more general, and not to more specific.

0 Kudos
Message 6 of 6
(2,679 Views)