LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Function that returns an objects default instance?

Solved!
Go to solution

Hi All

Is there a function or property node that returns the default instance of an object? I have a zipped project attached that shows

1. a workaround for the problem (Function folder) and

2. my use case (Example folder).

NathanDavis_5-1730913876217.png

Otherwise, is there a better approach to solving this?

Some pictures:

NathanDavis_4-1730913725020.png

NathanDavis_2-1730913550582.png

NathanDavis_0-1730913474473.png

NathanDavis_3-1730913570355.png

 

Thank you

Nathan Davis

0 Kudos
Message 1 of 6
(172 Views)
Solution
Accepted by topic author NathanDavis

Turn on scripting and you can get the default value.

paul_a_cardinale_0-1730917215994.png

 

Message 2 of 6
(153 Views)

Paul, thank you. I haven't used scripting before, this opens some possibilities to explore!

I have a revised project, with a follow up question. Replacing with your suggestion of the object property node, is there a property node that casts from the interface (slash parent class) to the more specific object that is the at Object input? I found that using the following property node into preserve run time class works for what I need (last picture), but curious if there's a property node that does this also?

NathanDavis_0-1730921437794.png

NathanDavis_2-1730921913783.png

NathanDavis_3-1730921926454.png

NathanDavis_1-1730921499476.png

 

Thank you

Nathan Davis

0 Kudos
Message 3 of 6
(132 Views)
Solution
Accepted by topic author NathanDavis

I think you will find that Paul's property node solution returns the default value of the control it refers to... NOT the default value of whatever is passed in.

 

What I think you want is a function that returns the default value of the class on the wire during runtime, whatever that is.

 

For example, if you have 3 classes that inherit... A is parent to B, B is parent to C.

 

If you pass B in, you want an instance of B to come out but with the default values for B instead of whatever was on the wire, yes?

 

If so, you are nearly there and just need to use the "Preserve run-time class" node combined with a default top-level Object:

Kyle97330_0-1730956427213.png

 

Because the top level object never has any data, and can never match any class it is cast to this will always generate an error, but even with the error (that you should ignore) the object out will always match the "target object" class on the wire passed in.

 

This is a side effect of the way "Dynamic dispatch" works in LabVIEW.  For a Dynamic Dispatch VI to be valid, the class passed in MUST be the class that is passed out, with no exceptions.  This is why there is the extra border on those wires:

Kyle97330_1-1730956688426.png

If you use a standard "To specific class" node, even though the wires are intact and the code is theoretically legal, it breaks the VI:

Kyle97330_4-1730957077416.png

You can see that the output wire is missing the special border, indicating that there is no guarantee that the class stays the same.

 

But as soon as it is replaced with "Preserve run-time class", it gets its special border back and the VI can run:

Kyle97330_5-1730957416355.png

 

Message 4 of 6
(97 Views)

Kyle, thank you for the detail! Very useful.

Not to add anything new, but to reiterate with pictures and an updated project:

NathanDavis_0-1730959283546.png

NathanDavis_2-1730959377564.png

NathanDavis_3-1730959407176.png

I understand to ignore the error in normal operation, but here for demonstration in case someone else is curious of the error generated.

 

I'll using this going forward, thank you for the clarity!

Nathan Davis

0 Kudos
Message 5 of 6
(90 Views)

@NathanDavis wrote:

Paul, thank you. I haven't used scripting before, this opens some possibilities to explore!

I have a revised project, with a follow up question. Replacing with your suggestion of the object property node, is there a property node that casts from the interface (slash parent class) to the more specific object that is the at Object input? I found that using the following property node into preserve run time class works for what I need (last picture), but curious if there's a property node that does this also?

...


No.  In fact property nodes don't perform functions. What you are looking for is an Invoke Node to execute a method.  However their is no method that does what you want.

Message 6 of 6
(66 Views)