Converting ushort? to ushort

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I need to convert a variable from uhsort? type to ushort.

The problem is that when I pass the variable of type ushort? to a method
that use a ushort type in his parameters the compiler throw an exception.

Does any body know how to do this?

Thanks, Robe.
 
Test for null then cast.

CallMethod( myShort != null ? (ushort) myShort : 0);

or whatever you want to do if myShort is null.
 
Running that example, I had to change the location of my cast and then
everything ran fine (assuming CallMethod takes a ushort as its
parameter):

CallMethod( (ushort)(myShort != null ? myShort : 0) );
 
CallMethod( myShort != null ? (ushort) myShort : 0);

Or simply

CallMethod(myShort ?? (ushort)0);


Mattias
 
Running that example, I had to change the location of my cast and then
everything ran fine (assuming CallMethod takes a ushort as its
parameter):

CallMethod( (ushort)(myShort != null ? myShort : 0) );

You might want to use the null coalescing operator to make things
easier:

CallMethod (myShort ?? 0)
 
Back
Top