08-11-2017 07:40 PM
This may be super simple but I feel like I'm missing something. Let's say I have a parent class "Car" with two child classes, "Ford" and "Chevy". In my application, the parent class itself is never used directly; it's always a child class that gets created depending on the user's selections.
For most methods, you will either use the parent class method or override it with a child class, and all of the selecting of information is done via dynamic dispatch. However, I have a specific application that will only fundamentally work with one specific child class (let's say Ford). In the initialization/construction of the "Car" object, I want to throw an error if a user tries to instantiate this sub-system with Chevy.
My first thought is to simply do an "Is Equal?" on the incoming "Car" class, checking it against a "Ford" constant- but I only want to detect which child class it is, not compare a constant to the newly created child class.
My next thought is to attempt a "To More Specific Class" and catch the error that is generated if I try to genericize (is that a word?) a Chevy class into a Ford class. This method works, but seems a bit unintuitive.
Last, I could make a method in the parent class that simply returns False unless it's overridden by a child class, and have the Ford class override the function and return True. However, this means I'm making two whole VI's to do a simple type check.
Am I overthinking this? Is the best way to attempt a conversion and check an error, or is there a function to compare a parent class to a specific child class?
I'm still new to LVOOP so forgive me if this is an obvious question. Thanks for the help.
Solved! Go to Solution.
08-11-2017 08:38 PM
BertMcMahan wrote:
Am I overthinking this? Is the best way to attempt a conversion and check an error, or is there a function to compare a parent class to a specific child class?
Yes, you are definitely overthinking this. Just try to convert to the more specific class and let that through an error if you are using an object that is not a child of the class you want.
08-13-2017 12:04 AM
Here's another way to do it:
08-14-2017 10:10 AM
@crossrulz wrote:
BertMcMahan wrote:
Am I overthinking this? Is the best way to attempt a conversion and check an error, or is there a function to compare a parent class to a specific child class?
Yes, you are definitely overthinking this. Just try to convert to the more specific class and let that through an error if you are using an object that is not a child of the class you want.
That's what I'm currently doing, but it's one of those situations where it didn't seem to me (an LVOOP rookie!) that the code was doing what it looked like; in other words, I feel like I would have needed to use a comment to explain what was happening.
@paul_cardinale wrote:
Here's another way to do it:
Thanks Paul, this method is a bit "larger" than I'd hoped but it's a bit more obvious what the code is actually trying to accomplish. I didn't know about that function.
Thanks to both of you for commenting, it's much appreciated!
08-14-2017 10:28 AM
@paul_cardinale wrote:
Here's another way to do it:
The problem with this method is that it does not work for any child classes of Ford.lvclass. Say we introduce Focus.lvclass. Should that also work in this application? This is where the To More Specific Class becomes simple as it is simply saying "do I have a Ford or any class that can be cast to a Ford?"
08-14-2017 10:31 AM
Ah I had not thought about further child classes, that is an excellent point! Thank you 🙂