01-11-2012 08:23 AM
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
Solved! Go to Solution.
01-11-2012 09:31 AM
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!
01-11-2012 09:39 AM
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.
01-12-2012 07:29 AM
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
01-12-2012 07:41 AM
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.
01-12-2012 08:00 AM
ok thank you for your help