LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Cast class A to class B

Solved!
Go to solution

I have the following situation.

 

Lets assume I have 2 classes called A and B.

 

Now we have the following facts:

  • Class B inherts from class A.
  • Class A has a function A1
  • Class B has a function B1
  • Class A has as content a numeric value
  • Class B has as content a boolean value (not relevant for my problem)
  • Function A1 has as input a string value and as output class A; It converts the string value to a numeric value and stores it.
  • Function B1 has as input a string value and as output class B; It calls A1 in the code.

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.

 

 

 

0 Kudos
Message 1 of 12
(4,479 Views)

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

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 2 of 12
(4,471 Views)

@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."

 

0 Kudos
Message 3 of 12
(4,463 Views)

No one?

0 Kudos
Message 4 of 12
(4,443 Views)

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.

0 Kudos
Message 5 of 12
(4,439 Views)

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."

0 Kudos
Message 6 of 12
(4,435 Views)

I think you may need some "Friends"Smiley Surprised  in the properties for Class A 


"Should be" isn't "Is" -Jay
0 Kudos
Message 7 of 12
(4,421 Views)

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.


___________________
Try to take over the world!
Message 8 of 12
(4,414 Views)

@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

 

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 9 of 12
(4,412 Views)
Solution
Accepted by topic author WG-

@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.

 

ab.png

 

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

0 Kudos
Message 10 of 12
(4,408 Views)