1

I am following an example and it has a class that goes like this:

public class Price
{

private decimal _sellingPrice;
private decimal _rrp;

public Price(decimal RRP, decimal SellingPrice)
{
            _rrp = RRP;
            _sellingPrice = SellingPrice;
}

}

This class is then constructed with values queried from a table using LINQ:

var products = from p in new ShopDataContext().Products                           
                           select new Model.Product
                           {
                               Id = p.ProductId,
                               Name = p.ProductName,
                               Price = new Price(p.RRP, p.SellingPrice)
                           };

On the example this seems to work, however I am getting this error:

Price = new Price(p.RRP, p.SellingPrice)
The best overload method match has some invalid arguments
Argument 1: cannot convert from 'decimal?' to 'decimal' 

The p.RRP and p.SellingPrice values are taken from a table as System.Decimal types and apparently nullable by default hence the exception, tho in the example this seems to run ok, so Why is that?. Is there anything I am missing?. I am experimenting with C# and I know its a strict language by default so there is no option to turn off and make this work in my understanding.

Thank you for your insight.

CoderRoller
  • 1,239
  • 3
  • 22
  • 39

2 Answers2

1

The problem is your query is returning a nullable decimal type rather than a decimal type. You need to modify your constructor like this:

public Price(decimal? RRP, decimal? SellingPrice) {
        _rrp = (decimal) RRP;
        _sellingPrice = (decimal) SellingPrice;
}

If you want to be thorough in checking for possible errors, you can use one of the techniques described in this article.

Community
  • 1
  • 1
Peter Gluck
  • 8,168
  • 1
  • 38
  • 37
  • Hi Peter, I noticed too that this change is needed so seems that the author missed the nullable decimal type on the constructor, I was just wondering if there was another way to make it work without doing that because it seemed to work well for him. – CoderRoller Sep 12 '12 at 21:31
  • Possibly the author used non-null database fields? It's not clear how it worked without more information. – Peter Gluck Sep 12 '12 at 23:38
  • The fields RRP and SellingPrice were set NULL on my table, therefore they were pulled out as decimal? on the LINQ query, the author set them non-null on the code samples (that I just found) :). Anyways I can workaround the problem with null or not null values. Both answers are correct but I am gonna give it to you since you pointed out that extra detail. Thank you guys. – CoderRoller Sep 13 '12 at 06:23
1

In C# decimal? cannot be converted implicitly to decimal. So the only way for you to work it out is to make the explicit convert. For example:

var products = from p in new ShopDataContext().Products                           
               select new Model.Product
               {
                   Id = p.ProductId,
                   Name = p.ProductName,
                   Price = new Price(
                        p.RRP ?? 0,
                        p.SellingPrice ?? 0)
               };
Andrei
  • 55,890
  • 9
  • 87
  • 108
  • @BobVale, thanks, still did not get used to this nice syntax. Updated the post! – Andrei Sep 12 '12 at 21:34
  • Hi BobVale and Andrei, what Andrei suggested: Price = new Price( p.RRP.HasValue ? p.RRP.Value : 0, p.SellingPrice.HasValue ? p.SellingPrice.Value : 0 ) Works too. Thank you guys. Seems that the author missed the conversion. – CoderRoller Sep 12 '12 at 21:39