LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Handling null values from .NET assemby methods

Hi,

 

I am not entirely sure if that belongs here but anyways, here is my Question:

 

Right now I am writing a .NET assembly in c# and my Question is, wether there is an convenient way to handle null values.

For example if a Method returns bool? it can be true, false or null. Also it should be distinguished between a string being empty ("") or null. Is that a Problem to handle in LabVIEW? Should I avoid these Methods?

Unfortunately I can not test this right now..

 

I only found out, that complex Types can not be handled, but null should not be a problem right? Just want to make sure.

 

Regards

Simon

 

0 Kudos
Message 1 of 4
(2,298 Views)

From your description I can safely assume you are talking about returning nullable types that close the Nullable<T> struct. Don't forget that C# has special syntax for dealing with nullable types but almost all of that syntactic sugar doesn't exist at the CLR / IL level. In fact, the CLR only changed minimally for the introduction of Nullable<T> - primarily around boxing/unboxing I believe. Nullable<T> types are completely different than reference types that can be null although the C# compiler lets you get away with treating them similarly (under the covers it does extra work for you).

 

LabVIEW hosts the CLR and thus is only aware of what gets compiled into IL, regardless of high-level language used. LabVIEW also doesn't work well with Generics but you can have a fully closed generic return type (it will have a strange name with Nullable in it in your case). LabVIEW will see this as an object just like any other .NET object, so it won't automatically know that your Nullable<bool> can be treated like a bool if HasValue=True. In order to access the contents of the returned type in LabVIEW you'd need to use the standard properties HasValue and Value and parse the result appropriately for your use case.

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

So what you are saying ist, as soon as i return a Nullable<T> type labview recognizes this as any .NET object and I have to unbox it accordingly to what I (have to) know is inside?

 

0 Kudos
Message 3 of 4
(2,263 Views)

Using your example of a Nullable<bool> type, the Value property of the open Nullable<T> type has the return type T - thus the closed type of Nullable<bool> has the return type of bool. So If you access the Value property in LabVIEW it will return bool and this is a primitive type that LabVIEW natively recognizes and converts to the LabVIEW data type of the same name.

 

Of course this is only specific for 'Nullable' Types that are implemented using the Nullable<T> struct and not reference-types which can be managed more easily in LabVIEW. For reference types LabVIEW will generate an error if the LabVIEW code attempts to operate on a null reference using a .NET method/property node but the code can also test for this via "Not a Number/Path/Refnum" comparison node and take the appropriate action, much like one might do in C# via the old pattern "if (ref == null) then throw new Exception() else ref.DoSomething()".

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