LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

When LabVIEW meets .NET - a (un)happy marriage (part 3) ?

Solved!
Go to solution

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.

0 Kudos
Message 1 of 9
(2,942 Views)

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


"Should be" isn't "Is" -Jay
0 Kudos
Message 2 of 9
(2,936 Views)

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.

0 Kudos
Message 3 of 9
(2,922 Views)

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

0 Kudos
Message 4 of 9
(2,907 Views)
Solution
Accepted by topic author fransv1963

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):

NET Array to LV Array.png

Message 5 of 9
(2,900 Views)

@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.

0 Kudos
Message 6 of 9
(2,887 Views)

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 ...

0 Kudos
Message 7 of 9
(2,862 Views)

@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.

0 Kudos
Message 8 of 9
(2,802 Views)

I'll keep this alternative in mind. Thanks.

0 Kudos
Message 9 of 9
(2,794 Views)