11-06-2024 11:26 AM
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).
Otherwise, is there a better approach to solving this?
Some pictures:
Thank you
Nathan Davis
Solved! Go to Solution.
11-06-2024 12:21 PM
Turn on scripting and you can get the default value.
11-06-2024 01:40 PM
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?
Thank you
Nathan Davis
11-06-2024 11:30 PM
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:
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:
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:
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:
11-07-2024 12:08 AM
Kyle, thank you for the detail! Very useful.
Not to add anything new, but to reiterate with pictures and an updated project:
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
11-07-2024 05:53 AM
@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.