LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to convert "instanceof" from java to Labview?

Solved!
Go to solution

Dear,

I would like to use a type comparison function that is "instanceof" in java but i do not know how to get a similar function in OOP labview ( i know for normal type but for classes I do not know). In my project I have 2 classes, one is the parent class (Parent) and the other is the child class (Child) which extends the parent class. I would like to convert this java code in labview :

 

Parent p1 = new Parent();
if (p1 instanceof Child
      do something;
 

Thanks

mnemo15

0 Kudos
Message 1 of 6
(3,275 Views)

I'm just gonna stab in the dark here:

 

1) If you do a "To More Specific Class", you can look at the error terminal to see if your cast worked (i.e. it will work if it's a valid class, i.e. if Parent is an instance of Child).

2) I think if you wire it to the "Get LV Class Path" VI, you will get the Child's class path.

 

You can also have a method in the base class that returns the name of the class, and then overload it in each of the child classes! Additionally, you could simply make your do something a member function of the base clase and then override it in the child class to do the thing it's supposed to do! (For instance, the base class method is empty, but the child class method 'does something'.

 

Just trying to help!

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

To More Specific Class will work in principle, but it will never work if you create LV code which is equivalent to the Java code you posted, and I'm assuming it won't work in Java either. The reason is that once p1 is created as a parent, it can never be an instance of the child. Of course, LV doesn't have variables like Java does, but there are ways where you can swap the value, and I'm assuming that's what you're planning on doing.

 

Note that there are cases where TMSC can't be used because you want to check casting to a specific class, but the wire type can't be of that class (because you're sharing that wire with other classes). In those cases you can use the Preserve Run-Time Class primitive instead of TMSC.


___________________
Try to take over the world!
0 Kudos
Message 3 of 6
(3,262 Views)

thanks for the answer, my java code was probably not a good exemple. In fact, i have 2 child class (child1 and child2) and I get an object p1 that I want to know if it is of the same class of child1. I think I will go for Preserve Run-Time Class Functio.

Let me know if you think I am wrong

 

cheers

mnemo15

0 Kudos
Message 4 of 6
(3,240 Views)
Solution
Accepted by topic author mnemo15

If you only want to check, you can use PRTC, but if you also want to use VIs from child1, you'll need to use TMSC.


___________________
Try to take over the world!
0 Kudos
Message 5 of 6
(3,237 Views)

ok thank you for your help

0 Kudos
Message 6 of 6
(3,233 Views)