Solved

Type of conditional expression cannot be determined because there is no implicit convertion between null and int

Posted on 2009-05-11
3
417 Views
Last Modified: 2013-11-07
I have a few fields that I'm reading from an excel file that could be blank (null) or with values (int or decimal).

Below is the code I've written, but it seemed like the conditional statement would not work as there is a type mismatch between the true/false statement. Is there a way to convert to a nullable type in the false statement?

Thanks in advance.  
decimal? paid = dReader["PAID_" + dynColsSuffix] == null ? null : Convert.ToDecimal(dReader["PAID_" + dynColsSuffix]);

Open in new window

0
Comment
Question by:ngobw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Accepted Solution

by:
steeza earned 125 total points
ID: 24352615
The compiler is unable to determine the type of the null part of your statement.
I don't have a C# compiler handy and haven't used it in a while, but you should have 2 ways of dealing with this:

a) Cast your null to a decimal? Ie. (decimal?)null
b) Let the statement return a decimal such as 0.0 when the column is null.
0
 
LVL 12

Assisted Solution

by:GuitarRich
GuitarRich earned 125 total points
ID: 24352652
Try this - or you could write your own ToDecimalNullable function that returned a decimal?

decimal? paid = dReader["PAID_" + dynColsSuffix] == null ? null : (decimal?)dReader["PAID_" + dynColsSuffix];
 

Open in new window

0
 
LVL 9

Expert Comment

by:tculler
ID: 24354989
I think what the problem here is that index of dReader is returning a struct of some kind. A struct cannot be compared to null. What is the exact type of "dReader"?

The last part of your statement (the Convert.ToDecimal) is not your problem here--there is always an implicit conversion from a non-nullable into a nullable (just not vice versa -- use the Coalesce operator ( "??")).
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question