From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Determine if an object is a member of a particular child class

A simplified example of my problem.

 

Two classes, Class A and Class B.

 

Class B inherits from Class A.

 

Class A methods:

Init

Acquire Image

Destroy

 

Class B methods:

Init *

Acquire Image *

Get Temperature

Destroy *

 

* denotes overridden method

 

Is there a way at run time of determining whether an object is a member of Class B or not so that I know whether I can run the Get Temperature method (the code using this object will have controls that are of Class A)? The only ways I can see of doing it at the moment are:

  • Add a Get Temperature method in Class A which doesn't do anything and just returns 0. Class B could then happily override that
  • Use the 'To More Specific Class' function, if it returns an error don't do anything (Currently my preferred option but I don't know if there is a performance hit)

Is there a better way/Am I just approaching inheritance wrong?

0 Kudos
Message 1 of 2
(565 Views)

Both ways you describe are perfectly valid solutions to your problem.

 

I would go with option 2 (to more specific + check error + run method if no error) because:

 -  It does not require Class A to have an empty method "Get Temperature". This method may not have sense for Class A.

 -  It works even if the object is a subclass of Class B;

 -  It does the check AND the cast at once, so you can directly use it with methods of Class B.

 

If you are worried about performance, you can also cast them once at the initialization of your application, and store them in a separate list of Class B objects.

I would do a benchmark with a big list of various objects (Class A and Class B mixed) to measure execution times. Maybe casting them each time you need it may be good enough...

0 Kudos
Message 2 of 2
(527 Views)