We help IT Professionals succeed at work.

c#: Error Trapping

trevor1940
trevor1940 asked
on
Hi,

I'm looking at ways to convert MGRS coordinates to Decimal  Latitude, Longitude  I've come across CoordinateSharp  the Developer Guide makes use of tryParse but dosn't seem say how to trap the errors nor dose the dotnetfiddle example

I also note the output is Degree minute seconds not digital

The Developer Guide Suggests "c.FormatOptions.CoordinateFormatType = CoordinateFormatType.Degree_Decimal_Minutes;"


However this Errors

		string mgrs = "16R CV 05774 44722";
		Coordinate c;
		if(Coordinate.TryParse("16R CV 05774 44722", out c))
		{
			c.FormatOptions.CoordinateFormatType = CoordinateFormatType.Degree_Decimal_Minutes;

			Console.WriteLine(c);
		}

Open in new window


So how do I rewrite the above trapping errors and output to  Decimal  Latitude, Longitude variables?
Comment
Watch Question

Senior Developer
Commented:
TryParse() methodes return false, when they cannot convert the value. So you simply need an else-branch for your if.

I also note the output is Degree minute seconds not digital
This observation is not correct. The output is a Coordinate object. What you see is just the default formatting. For accessing the actual values, you need to use c.Latitude and c.Longitude and their methods.

Author

Commented:
OK so this works

		Coordinate c;
		if(Coordinate.TryParse("crap 16R CV 05774 44722", out c))
		{
			string Lat = c.Latitude.DecimalDegree.ToString();
			string Long = c.Longitude.DecimalDegree.ToString();
			Console.WriteLine(Lat + ", " + Long);
		}
		else{
				Console.WriteLine("Error!");
		}

Open in new window


However it doesn't give any indication of what the error is In this case it's badly formatted MGRS, I've seen examples out of range where user has typed the wrong values correctly
ste5anSenior Developer

Commented:
At a short glance at the parsers, they internally only use the TryParse pattern and don't bubble up any message where it failed. So in the current state, it seems, that you cannot get that information.

But as it is on GitHub, you can add this feature if you like.

Author

Commented:
Thanx I find it quite odd not being able tell errors