Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Implicitly coverting a double? to double

Status
Not open for further replies.

zyman10

Technical User
Dec 7, 2008
41
US
I am starting to learn ASP.NET with C# and I am trying to create the Data Access Layer and Business Logic Layers for my .aspx page.

In my DAL (.xsd) I have a Table called Parts that contains field "Weight" that is a System.Double

In my BLL I am making a function that eventually Add Parts to my Parts table. The first thing I want to do is just check that values that my AddParts function is getting are null, and if so then it will pass null to the DAL INSERT function. Needlessly to say I am having issues. Here is some of my code from the BLL in the AddParts function:

Code:
[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
    public bool AddPart(string partNum, string manufact, string model, string description, string upc, double? weight, double? height,
        double? width, double? depth, int? trackSerial, string pictureURL)
    {
...
...
if (weight == null)
    part.SetWeightNull();
else
    part.Weight = [red]weight;[/red]
...
}
The intellisense has highlighted the red part above as an issue. When I hover over it, it says
"parameter weight? double
Error: Cannot implicitly covert 'double?' to 'double'. An explicit casting exists (are you missing a cast?)"

I am not really sure what is going on here. Maybe it has to do with the '?' after the double? in the function declaration, or maybe not. Does anyone know what's going on?

Do I even need the '?' If not, what is it for? I just found this type of code on the site I am using to learn ASP with C# asp.net/learn/ I haven't quite figured out why they use the '?' after some fields and not others.

Can anyone help me with understanding these two things and why I am getting this error...

Thanks to any help.


 
why was googling this so terrible until i posted this. jesus. i'm sorry for bothering everyone. i just found the answer.

the '?' allows for nullable types in C#. the reason it was trying to cast was because i needed to grab the value from the variables like weight.Value; instead of just weight.

again, sorry for any confusion.

thanks for anyone who viewed this.
 
Not a problem. Thanks for posting what you found. It will help someone in the future. It helped me, because I am a VB guy not a C# guy and I am learning as I am going along.

thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top