05-21-2012 09:33 AM
I'm using a .NET assembly where one of the member functions returns a System.Array (partly due to the fact that LabVIEW 2011 does not allow to instantiate generic classes).
This return value should be casted to System.Double[] in order to map onto a double array in LabVIEW.
I learned that it is possible to cast an object to a more specific (and even abstract) type, but I don't see how it is possible to specify a System.Double[] as the target class as part of a "To More Specific Class".
Possibly I'm overlooking something or there is another way to achieve this (beyond the workaround to write a simple helper class which performs this cast in .NET).
Any help being much appreciated.
Solved! Go to Solution.
05-21-2012 10:09 AM
The abstract classes cannot be directly constructed. I know it sounds somewhat counter-intuitive but to get an abstract class you involk the create instance method
05-21-2012 11:52 AM
See here for a similar question, and for an example where I show to use the CreateInstance method, as Jeff alluded to. This is by .NET design.
05-21-2012 01:05 PM
Probably I'm missing the clue of both replies, sorry.
I already had a look at the Array.CreateInstance-related topic you refer to, but - unless I'm missing the clue - the problem is different here : the .NET assembly returns a System.Array where I know the underlying type is double[].
The reason why Array is returned instead of double[] is simply due to the fact that different data types can be returned depending on the subclass that implements that function.
Can you please extend a bit on how you'd perform the cast to a double[] to get the array of doubles into LabVIEW (compared to my simple .NET helper class which performs the cast in .NET) ?
05-21-2012 01:27 PM
If I understand correctly, you just need to do the reverse of what's shown in smercurio's link. That is, take the .NET array, retrieve the length, and wire that to the N terminal of a for loop. Then, in the for loop, use the Array GetValue function to retrieve the DBL value from that array element and use .NET Object to Variant, followed by Variant to Data, to convert to a Double. Wire that to the edge of the for loop and you'll get your array in LabVIEW. It will look something like this (ignoring the first few functions that just build a dummy array of empty elements):
05-21-2012 02:42 PM
@nathand wrote:
If I understand correctly, you just need to do the reverse of what's shown in smercurio's link. That is, take the .NET array, retrieve the length, and wire that to the N terminal of a for loop. Then, in the for loop, use the Array GetValue function to retrieve the DBL value from that array element and use .NET Object to Variant, followed by Variant to Data, to convert to a Double. Wire that to the edge of the for loop and you'll get your array in LabVIEW. It will look something like this (ignoring the first few functions that just build a dummy array of empty elements):
Basically, yes, that's all that you should need to do. The output of the .NET function should replace everything to the left of the GetLength call, as the function will be returning the System.Array datatype. Thanks for providing an explicit reversal of the example, Nathan.
05-22-2012 02:42 AM
Thank you both for the additional information (and the example code) ... it confirmed my assumption, i.e. that one has to convert one element at the time.
It's good to know that it is possible in LabVIEW, but for the time being I prefer the alternative solution of a simple helper class in .NET to perform the cast.
I hope it is ok if I tag the example LabVIEW code of Nathan as the solution ...
07-15-2012 01:01 AM
@Frans Verbeyst wrote:
Thank you both for the additional information (and the example code) ... it confirmed my assumption, i.e. that one has to convert one element at the time.
I realize this is too late to be helpful, but it turns out you do not need to do this one element at a time. You can wire the .NET System.Array directly to .NET Object to Variant, then wire that variant to Variant to Data with the datatype set to be an array. This might be the solution you wanted.
07-15-2012 02:22 PM
I'll keep this alternative in mind. Thanks.