LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I check an object is an instance of any type within an array of related types?

Solved!
Go to solution

In LabVIEW, it's possible to check the runtime type of an object using To More Specific Class.vi. One usage of this would be to perform a safety check if some kind of class uses instances of another kind of class but is only able to handle a subset of that class' child types.

 

For instance, let's say you had Beverage.lvclass, which represents an abstract type of product, and several subclasses: Coffee.lvclass, Tea.lvclass and Soda.lvclass.

 

We also have a Person.lvclass which can drink beverages. A person also has preferences about the drinks they do or do not like:

 

 

Attached is an implementation of this in LabVIEW.

 

In Person.lvclass : Drink.vi, I have the following code. For whatever reason the wire appears broken in these snippets but it's all fine in the actual code.

 

GoodDrink.png

 

BadDrink.png

 

In Scenario.vi, I have the following code:

 

 Scenario.png

 

What I'm finding is that no error is generated and instead I get "Yum, I love tea!", "Yum, I love coffee!" and "Yum, I love soda!". My guess is that To More Specific Class.vi casts to the static type of the "target class" input wire rather than its runtime type - and because I'm passing in elements from an array of types, the wire's static type is upcasted to the most specific type that is a superclass of the types in the array - which would be Beverage.lvclass. And so the cast is trying to determine if an object of static type Beverage.lvclass is an instance of Beverage.lvclass, which will of course be the case all the time.

 

Is there any way to make LabVIEW check against the most specific runtype type of an object? As in, is there something I could do that, in this example case, would allow me to get the required behaviour of Joe throwing an error when he's made to drink soda? Or is this another limitation of LabVIEW that I'm going to have to live with?

0 Kudos
Message 1 of 3
(4,465 Views)
Solution
Accepted by topic author DavidFCannock

Your guess seems reasonable. I can't check right now, but there's a primitive called preserve run time type, which should do what you want.


___________________
Try to take over the world!
Message 2 of 3
(4,440 Views)

@tst wrote:

Your guess seems reasonable. I can't check right now, but there's a primitive called preserve run time type, which should do what you want.


Cheers, that seems to have got it! I've never really looked at Preserve Run-Time Class before, but it seems to do the right thing.

WorkingCode.png

 

WorkingScenario.png

Message 3 of 3
(4,429 Views)