From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, 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: 

Cannot convert LabView Double type to .Net System.Nullable[System.Decimal] type

Solved!
Go to solution

Hello all,

 

My LabView Project uses a .Net DLL. In the DLL there is a class "A2InParam". This class has a member "ValueDecimal" that has "System.Nullable[System.Decimal]" type.

pic1.png

 

 

I need to write something to that member.

I convert the "Double" constant (also tried "Single", "Extended") to .Net object and then cast it to "System.Nullable[System.Decimal]" type (using a constant, that was created by "create constant" function from "ValueDecimal").

 

But I get an error in "More Specific Class":

Type mismatch: Object cannot be cast to the specified type.

 

If I work with a type "System.Nullable[System.Int32]" it works fine.

pic2.png 

 

 

Small test program (LabView2015) I have attached.

 

Thank you

0 Kudos
Message 1 of 4
(2,943 Views)
Solution
Accepted by topic author Jason_Tut

Don't forget that LabVIEW types and .NET types are not the same thing. They are implemented differently and act differently. A LabVIEW Boolean, for example, is not the same as a .NET Boolean even though they have the same name. The only reason you can pass some primitive LabVIEW types to .NET properties and methods expecting .NET types is that LabVIEW contains some innate "conversions" between it's primitive types (all those described in the help for "To .NET Object") and the similarly named/purpose .NET types. Some conversions are elementary (such as value-based types like single, double etc.) whereas some are more complex (such as Timestamps).

 

The .NET Decimal type is different from other "number" types in that it isn't a CLR primitive type.  LabVIEW has no innate conversions from any of its primitive types to the .NET Decimal type.

 

Also working with generics (such as Nullable<T>) can be confusing in LabVIEW depending on how you use it. There are limitations as discussed here: https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z000000P8iWSAS&l=en-NZ. If you have control over the contents of the .NET dll I'd suggest limiting your API surface to non-generic types - your LabVIEW program has no way of creating a non-null Nullable<T> type so having a Nullable API is a bit wasteful.

Message 2 of 4
(2,907 Views)

Actually you can create just about every possible .Net type in LabVIEW too, but that requires to invoke .Net Reflection methods and gets very quickly very complex. Even many .Net developers do not really know much more about it than that it exists. Going down that path is covered with quite a bit of pain and will cause your code to be rather difficult to maintain. In 99.9% of the cases it is not worth the price.

Rolf Kalbermatter
My Blog
0 Kudos
Message 3 of 4
(2,883 Views)

Yes reflection is an option for (almost) every other generic type. And yeah, just don't go there unless you want to be pulling your hair out Smiley Happy

 

But for creating instances of Nullable<T> it appears especially difficult due to specific in-built CLR behavior around instances of Nullable<T> being boxed to T. This is an interesting read on this (see here https://bradwilson.typepad.com/blog/2008/07/creating-nullab.html).

 

@Jason_Tut - you should be able to create an instance of Decimal (via the Decimal(double value) constructor) and pass that directly to the method. The CLR will unbox this value to a Nullable<Decimal> instance with the property HasValue=True and the call will work. Here's a quick example:

 

2019-01-11 07_47_11-Win7x64 NI2016 - VMware Workstation.png

 

 

@rolfk - as you've pointed out my original post has a typo (serves me right for not properly checking!). It should have said "your LabVIEW program has no way of creating a null Nullable<T> type" meaning that, if you control the API surface and intend to use it specifically with LabVIEW, consider whether you actually need Nullable<T> or could just use T instead since you can't pass in "null" .

0 Kudos
Message 4 of 4
(2,870 Views)