0

I have a property like this:

 public float Lat{
        get {
            float lat;
            if (!float.TryParse(hdnLat.Value, out lat))  
            {
                throw new NotImplementedException();
            }

            return lat;
        }
        set { 
            hdnLat.Value = value;  // Line 43
        }
    }

I got Latitude and Longitude from Google Maps and i get the cordinates from two asp.net hiddenfields.

<asp:HiddenField ID="hdnLat" runat="server" />
<asp:HiddenField ID="hdnLng" runat="server" />

I store my latitude and longitude as float in my databas so i have to convert it to float right?

How can i convert my cordinates to correct format?

Visual Studio givs me this error:

Can not implicitly convert type double to string Line 43

How can i solve this problem?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
tord
  • 1
  • 1
  • 2
  • 2
  • 4
    [Do not throw NotImplementedException](http://stackoverflow.com/questions/410719/notimplementedexception-are-they-kidding-me), unless you plan on implementing it later. – R. Martinho Fernandes May 16 '11 at 11:33
  • 1
    don't throw a NotImplementedException - that's for when you don't write one method in an interface. Use ArgumentException - that's what it's for. – Kate Gregory May 16 '11 at 11:34
  • 1
    @Kate: No, that's not for that. First, *don't do that*. If you don't want to implement a method in an interface, you probably should not be implementing the interface. And if you really need to do that, throw NotSupportedException. Code that is complete should **never throw NotImplementedException**. And there are no exceptions to that rule. – R. Martinho Fernandes May 16 '11 at 11:35
  • @Martinho yes I know, I meant "that's what gets put in there for you until you implement". Anyway the point is it sucks for "hey your string can't be parsed to a number" and ArgumentException is the better choice – Kate Gregory May 16 '11 at 11:37
  • 2
    @Kate: getters have no arguments so ArgumentException makes no sense. You shouldn't throw anything at all from a getter. But if there is no other choice (but probably there is), this code should probably throw FormatException (which is what float.Parse would throw). – R. Martinho Fernandes May 16 '11 at 11:39
  • @Kate, @Martinho: float.Parse() throws a `System.FormatException` when parsing fails. I guess this would also be appropriate here. – M4N May 16 '11 at 11:45
  • 2
    Yeah, `FormatException` is the right choice (although, in general, Martinho is correct: getters should not throw exceptions). But it doesn't make a lot of sense to call `TryParse` and then throw if it fails. **That's what the `Parse` method already does for you.** – Cody Gray - on strike May 16 '11 at 11:46
  • @tord: What is the type of `hdnLat` here ? – Akram Shahda May 16 '11 at 12:07

1 Answers1

8

Since hdnLat.Value is of type string, when you assign to it, the item you assign must also by of type string. So if you want to assign value, you have to convert it into a comparable item of type string. And you can do that like this:

hdnLat.Value = value.ToString();

Which is exactly what the error message "can not implicitly convert type double to string" is trying to tell you. You should read this message as "I see you're trying to use a double where I expected a string. I tried to figure out how to convert it, but I cannot. Could you tell me explicitly how to convert it?"

Shalom Craimer
  • 20,659
  • 8
  • 70
  • 106
M4N
  • 94,805
  • 45
  • 217
  • 260
  • 1
    @Cody: I think that's fair if you compare with the 2 upvotes for `"" + value` ;-) – M4N May 16 '11 at 11:42
  • 3
    @Akram: I think answers that include *only* code with no *explanation* are basically worthless. Thus, I don't think they deserve any upvotes. Depending on my mood, I have been known to downvote such answers. They don't actually tell the asker what the problem is, and they encourage people to copy and paste code directly into their source, which is never a good idea. *Good* answers do exactly the same thing as good questions. They explain what the problem is, what causes it, and only *then* how to fix it. – Cody Gray - on strike May 16 '11 at 11:54
  • @Cody Gray: I totaly agree with you. To be honest, I haven't read the question body yet !! – Akram Shahda May 16 '11 at 11:58
  • @Cody: that's why I cringe when I see questions whose answers can be just a piece of code without explanation, like "I need a regex to do X. Please write it for me." – R. Martinho Fernandes May 16 '11 at 12:01
  • @Martinho: I've stopped cringing so much now that we get 50 close votes per day. It was a real buzzkill, though, when I used to run out. – Cody Gray - on strike May 16 '11 at 12:01