Avatar of trevor1940
trevor1940
 asked on

c#: Error Trapping

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?
.NET ProgrammingC#

Avatar of undefined
Last Comment
trevor1940

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
ste5an

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
trevor1940

ASKER
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
ste5an

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.
trevor1940

ASKER
Thanx I find it quite odd not being able tell errors
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23