?
Solved

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

Posted on 2009-05-11
3
Medium Priority
?
427 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 375 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 375 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

719 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