LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Programmatically determining the child class type of a dynamic parent wire

Solved!
Go to solution

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.

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

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.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 2 of 6
(3,824 Views)
Solution
Accepted by topic author BertMcMahan

Here's another way to do it:

Untitled.png

"If you weren't supposed to push it, it wouldn't be a button."
Message 3 of 6
(3,681 Views)
@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:

Untitled.png


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!

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

@paul_cardinale wrote:

Here's another way to do it:

Untitled.png


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


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 5 of 6
(3,607 Views)

Ah I had not thought about further child classes, that is an excellent point! Thank you 🙂

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