From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.
We appreciate your patience as we improve our online experience.
From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.
We appreciate your patience as we improve our online experience.
01-31-2022 08:44 AM
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.
Solved! Go to Solution.
01-31-2022 09:06 AM
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.
01-31-2022 09:13 AM
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.
01-31-2022 10:40 AM
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?
01-31-2022 10:45 AM - edited 01-31-2022 10:52 AM
@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.
01-31-2022 11:43 AM
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.