473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Converting ushort? to ushort

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.
Mar 14 '06 #1
6 4387
Robe,

What is the error that you are getting?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Robe" <Ro**@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
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.

Mar 14 '06 #2
Test for null then cast.

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

or whatever you want to do if myShort is null.
Mar 14 '06 #3
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) );

Mar 14 '06 #4
>CallMethod( myShort != null ? (ushort) myShort : 0);

Or simply

CallMethod(myShort ?? (ushort)0);
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 14 '06 #5
<d2*****@yahoo.com> wrote:
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)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 14 '06 #6
If I'd known that, I'd have said it - thank you.
Mar 15 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Axel Dahmen | last post by:
Hi, I want to start a technical discussion on the fact that C# doesn't define any mathematical operators on other integral types than int. Simple things like the following just aren't possible...
4
by: JuLiE Dxer | last post by:
Perhaps, I skipped over learning this particular tidbit. I ran across an odd error while trying to add to ushort variables together and assigning it to a 3rd ushort variable. I get this...
12
by: AMP | last post by:
Hello, I have in c: WORD calcChecksum(BYTE data, WORD length) { WORD* i_data; WORD checksum= 0; BYTE i= 0; i_data= (WORD*)data;
4
by: rakesh.Mysore | last post by:
HI I have a problem in casting short to ushort in my project. i have a ushort value of 38,143 now it is converted into short value of 27,393 and stored in variable.(when i casted ushort to...
1
by: fishspm | last post by:
hello everyone in my source I receive a byte array (for eg: 0xED, 0xFF, 0x01, 0x3D). is there an easy way to convert this byte array to a ushort/uint16 array (0xffed, 0x3d01)? thank you very...
0
by: anide | last post by:
Hi all I’ve some problem, I’m trying to converting a sorting algorithm from C++ to C#. In C++ I’ve compiled it using MSVC and its working properly, and in C# I’m using .NET Framework 2.0 (Visual...
6
by: canoewhiteh2o | last post by:
I am converting a couple of C header files to C#. It is mainly just a bunch C structs but I am not sure how to handle the #ifdef and #ifndef in C#. For example: #ifndef DATE_TIME #define...
1
by: xlar54 | last post by:
I have some code that ends up looking alot like: ushort x = (ushort)(m + b); whereas both m and b are ushorts also. This is due to the automatic casting of values to int. Some quick...
8
by: Mark Main | last post by:
I just bought Visual Studio 2008, I'm new to C# and trying to learn it. I'm stuck and would appreciate some help. I need to make the fastest code I can to work with a large key (it's 200 bytes...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.