05-28-2011 09:49 AM
I have the following situation.
Lets assume I have 2 classes called A and B.
Now we have the following facts:
Now the problem lays in function B1. When I call the function A1 it gives me as output class A. Now I want to convert this class from A to B. Because B inherts (the data from class A is also in class B?) from A this should be possible? How do I do this???
Example file included. Run B1 to see the problem.
Solved! Go to Solution.
05-28-2011 10:38 AM
NO LV 2010 on this machine but this code demonstrates how I find plug-in and cast the wire to the class using the "To more Specific Class" function.
The returned error is checked to determine if the operation was succesful. It is only successful for the target class or children of the target class.
Ben
05-28-2011 11:12 AM
@Ben sorry maybe I was unclear but I know I should use the "to more specfic class" but it currently gives me the error:
"LabVIEW: Bad type cast. LabVIEW cannot treat the run-time value of this LabVIEW class as an instance of the given LabVIEW class."
05-28-2011 02:24 PM
No one?
05-28-2011 02:44 PM - edited 05-28-2011 02:50 PM
Also no access to LV2010 right now so I can't test, but I believe the function you need is "Preserve Run-Time Class" in place of "To More Specific Class."
EDIT: To clarify, I think what you need to do is take the wire going into A.vi and fork it to the Target Object terminal of Preserve Run-Time Class, which should be placed after the output of A.vi.
05-28-2011 02:50 PM - edited 05-28-2011 02:52 PM
I tried that as well and it didn't made difference 😞 The error is then: "LabVIEW: The class of object in did not match the class of target object at run-time."
05-28-2011 05:24 PM - edited 05-28-2011 05:25 PM
I think you may need some "Friends" in the properties for Class A
05-29-2011 01:11 AM
I can't see the code either, but I think the people who replied might be missing the point here. If I understand the description correctly, both A1 and B1 do not have a class control as an input, and this reply is based on that.
So, first, just to clarify - you CAN NOT convert an A to a B. Ever. LabVIEW won't let you, because while B is always an A, A is not always a B. A might be just an A, or it could be C, F or G (other classes inheriting from A). The only way LV will let you make the conversion is if this A is already a B.
If you want to construct a B from an A, you need to write special code which will extract all the relevant info from the A input and shove it into a NEW B object.
Second, in the case of your functions, you might wish to give the functions a class input. If your VIs are dynamic dispatch (and if they actually do have the same name and are not actually A1 and B2), then you'll need it anyway to make them work, and that will solve that issue, because the type is propagated automatically. If they're not and you want to use them as a constructor to create a new object, you can use the VI in A with a class input just to initialize the object. Then you can call that init VI from your class B init VI and give it the B object you created.
05-29-2011 01:29 AM
@tst wrote:
I can't see the code either, but I think the people who replied might be missing the point here. If I understand the description correctly, both A1 and B1 do not have a class control as an input, and this reply is based on that.
So, first, just to clarify - you CAN NOT convert an A to a B. Ever. LabVIEW won't let you, because while B is always an A, A is not always a B. A might be just an A, or it could be C, F or G (other classes inheriting from A). The only way LV will let you make the conversion is if this A is already a B.
If you want to construct a B from an A, you need to write special code which will extract all the relevant info from the A input and shove it into a NEW B object.
Second, in the case of your functions, you might wish to give the functions a class input. If your VIs are dynamic dispatch (and if they actually do have the same name and are not actually A1 and B2), then you'll need it anyway to make them work, and that will solve that issue, because the type is propagated automatically. If they're not and you want to use them as a constructor to create a new object, you can use the VI in A with a class input just to initialize the object. Then you can call that init VI from your class B init VI and give it the B object you created.
What he said.
Ben
05-29-2011 04:24 AM - edited 05-29-2011 04:31 AM
@tst. You are right A1 and B1 do not have a class control as input. And a real shame that A can not be casted to B.
But I dont understand why you are saying "because while B is always an A, A is not always a B." See image above and let me explain...
A is a subset of B and B is a not subset of A. The first because of B inherts from A. The latter because the boolean value of B is not in A. Futher B is a subset of B. When casting B to A, one loses data (which works), the boolean value in this case. But when casting A to B I was expecting that it would be a union between A and the difference from B and A, which is in the end B. (A ∪ (B \ A)). Or in other words I was expecting it would just combine A and the default boolean value from B which results in B.
Anyway I already solved this now. I just removed the inheritance and made A a element of B (meaning B now has as content a boolean value and A).