473,386 Members | 1,817 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,386 software developers and data experts.

Decimal nullable problem

Hi all,
I have an expression like this:

decimal? ValoreComplessivoNettoDelfondoValue = (totaleAttivitaValue.HasValue
|| totalePassivitaValue.HasValue) ? ((totaleAttivitaValue ?? 0) -
(totalePassivitaValue ?? 0)) : null;

where every variable is of type Decimal?.

But VS 2005 returns me the error "Impossibile implicit conversion between
decimal and null".

Where's the problem?

Thanks a lot.
--
Luigi
Sep 25 '08 #1
4 6120
It happens that Luigi formulated :
Hi all,
I have an expression like this:

decimal? ValoreComplessivoNettoDelfondoValue = (totaleAttivitaValue.HasValue
>>totalePassivitaValue.HasValue) ? ((totaleAttivitaValue ?? 0) -
(totalePassivitaValue ?? 0)) : null;

where every variable is of type Decimal?.

But VS 2005 returns me the error "Impossibile implicit conversion between
decimal and null".

Where's the problem?

Thanks a lot.
use "(decimal?)null" instead of "null", then it will compile.

The problem is that "(totalePassivitaValue ?? 0)" has a resulttype of
"decimal", not "decimal?". So when the compiler tries to decide on a
returntype of the conditional expression (..?..:..) it sees "decimal"
in the "true expression" and a null in the "false expression". And it
can't match the two.
If you cast the null to a nullable decimal, then the compiler sees
"decimal" and "decimal?" and knows there is an implicit conversion, so
the result-type will be "decimal?".

Hans Kesting
Sep 25 '08 #2
On Sep 25, 9:52*am, Luigi <ciupazNoSpamGra...@inwind.itwrote:
I have an expression like this:

decimal? ValoreComplessivoNettoDelfondoValue = (totaleAttivitaValue.HasValue
|| totalePassivitaValue.HasValue) ? ((totaleAttivitaValue ?? 0) -
(totalePassivitaValue ?? 0)) : null;

where every variable is of type Decimal?.

But VS 2005 returns me the error "Impossibile implicit conversion between
decimal and null".

Where's the problem?
Well, that many conditionals and null-coalescing operators makes the
code very difficult to read for a start. I would break it up into
separate statements - I suspect you'll find the problem goes away then
anyway. I believe the issue is demonstrated by this rather simpler
code:

decimal? foo = true ? 5m : null;

The types of the rightmost two operands are "decimal" and "<null>" and
there's no implicit conversion from either one to the other. If you
cast either of the operands to "decimal?" it will work fine:

decimal? foo = true ? (decimal?) 5m : null;
or
decimal? foo = true ? 5m : (decimal?) null;

You could do the same with your original code - casting the null is
probably simpler than casting the calculation.

Jon
Sep 25 '08 #3
"Hans Kesting" wrote:
use "(decimal?)null" instead of "null", then it will compile.

The problem is that "(totalePassivitaValue ?? 0)" has a resulttype of
"decimal", not "decimal?". So when the compiler tries to decide on a
returntype of the conditional expression (..?..:..) it sees "decimal"
in the "true expression" and a null in the "false expression". And it
can't match the two.
If you cast the null to a nullable decimal, then the compiler sees
"decimal" and "decimal?" and knows there is an implicit conversion, so
the result-type will be "decimal?".
Thank you Hans, now it's working (with a cast).

Luigi
Sep 25 '08 #4
"Jon Skeet [C# MVP]" wrote:
Well, that many conditionals and null-coalescing operators makes the
code very difficult to read for a start. I would break it up into
separate statements - I suspect you'll find the problem goes away then
anyway. I believe the issue is demonstrated by this rather simpler
code:

decimal? foo = true ? 5m : null;

The types of the rightmost two operands are "decimal" and "<null>" and
there's no implicit conversion from either one to the other. If you
cast either of the operands to "decimal?" it will work fine:

decimal? foo = true ? (decimal?) 5m : null;
or
decimal? foo = true ? 5m : (decimal?) null;

You could do the same with your original code - casting the null is
probably simpler than casting the calculation.
Well, thank you Jon, another good point of view to keep in mind.

Luigi
Sep 25 '08 #5

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

Similar topics

4
by: Mark | last post by:
We have a SQL Server table with a decimal column that is not required. We're building a .NET application for data entry. If the value is optional, but a decimal is strongly typed in C# and cannot...
3
by: Gustaf Liljegren | last post by:
I searched for previous answers on this, but couldn't find something fitting. I need advice on how to store decimal numbers with possible null values in memory. The numbers may be negative, so...
8
by: Sam Kong | last post by:
Hello, I want to define a generic class which should accept only nullable types or reference types. What's the best way to costrain it? --------- class MyClass<T>{ ...
3
by: Mike P | last post by:
What are nullable types used for? The only use I can think of is where I have a method like this where some values being passed to it may be null : public int AddUser(int? UserRegion, string...
5
by: Franky | last post by:
Can a Decimal variable be set to "Not a Number" I've been looking in the help and can't find any reference to that. Thanks in advance
5
by: =?Utf-8?B?emlubw==?= | last post by:
in .net 2.0, the class (myClass) contains a property defined as: private _creationDate as As Nullable(Of DateTime) Public Property CreationDate() As Nullable(Of DateTime) Get If...
6
by: Tony Johansson | last post by:
Hello! I'm reading in a book called Visual C# 2005. In the chapter about Generics there ia a section about Nullable types. Here is the text that isn't complete true I think. It says: "You...
2
by: =?Utf-8?B?THVpZ2k=?= | last post by:
Hi all how can I write a method that returns me true if is possible to cast an object to decimal nullable and false if not? Like public bool CanConvert(object obj) { return true if obj is...
5
by: =?Utf-8?B?THVpZ2k=?= | last post by:
Hi all, I have this snippet that don't compile: Math.Ceiling(-voceValue.Value - 0.5M); because the field Value of the class voceValue is decimal nullable. How can I solve? Thanks a lot.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.