Can not convert double to float

Posted on 2012-08-20
Last Modified: 2012-08-26
I am tring to get a double into a the setter of a property which accepts a double.  But I get the compiler error: Can not convert double to float.

  myObject.hours = Convert.ToSingle(_view.NumUpDownMinutes.Value) / 60.0;

How can I get past this problem??
Question by:newbieweb
    LVL 16

    Accepted Solution


    can you try like this?

    myObject.hours = (float)Convert.ToDouble(Convert.ToSingle(_view.NumUpDownMinutes.Value) / 60.0);

    Open in new window

    LVL 40

    Assisted Solution

    by:Jacques Bourgeois (James Burger)
    First of all, why do you Convert.ToSingle before doing the division?

    If your property accepts a Double as you state, you should not get the error. The result of a division in C# is a Double. I thus suspect that the property has been defined as a float, not as a Double.

    If you have access to the source code for the hours property, then simply redefine it as a Double and you won't have any problem.

    Author Closing Comment

    I had actually been successful using this conversion with multiple nested conversions.  I am not able to change the type in the database, which is what Entity Framework uses to create the objects.

    I am up and running...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Suggested Solutions

    After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now