nal
nal

Reputation: 119

Float cast error

I defined Latitude Float(24) and Longitude Float(24) in my database(dinner table). And then when I tried to use them, the code shown as following in my model

  public class JsonDinner
{
    public JsonDinner(){}
    public JsonDinner(Dinner dinner)
    {
        DinnerID = dinner.DinnerID;
        EventDate = dinner.EventDate.ToString();
        Latitude =  dinner.Latitude;
        Longitude = dinner.Longitude;
        Title = dinner.Title;
        Description = dinner.Description;
        RSVPCount = dinner.RSVPs.Count;
        Url = "dinner/details/" + dinner.DinnerID.ToString();
    }

        public int    DinnerID    {get; set;}
        public string EventDate   {get; set;}
        public float  Latitude    {get; set;}
        public float  Longitude   {get; set;}
        public string Title       {get; set;}
        public string Description {get; set;}
        public int    RSVPCount   {get; set;}
        public string Url         {get; set;}
}

It said

Cannot implicitly convert 'float' to 'float'. An explicitly conversion exists(are you missing a cast)?

What I did wrong? I even didnt use any casting.

Upvotes: 1

Views: 201

Answers (1)

DonBoitnott
DonBoitnott

Reputation: 11025

Coming out of the database, I believe float equates to C# double. So make them:

Latitude =  (float)dinner.Latitude;
Longitude = (float)dinner.Longitude;

But understand that you're potentially reducing precision, so truncation/rounding becomes a real possibility.

Upvotes: 1

Related Questions