15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

962 ❘ ChaPTer 34 .net prOGrAmminG with sQl server<br />

}<br />

return null;<br />

char northSouth = longitude > 0 'N': 'S';<br />

char eastWest = latitude > 0 'E': 'W';<br />

int longitudeDegrees = Math.Abs(longitude) / 3600;<br />

int remainingSeconds = Math.Abs(longitude) % 3600;<br />

int longitudeMinutes = remainingSeconds / 60;<br />

int longitudeSeconds = remainingSeconds % 60;<br />

int latitudeDegrees = Math.Abs(latitude) / 3600;<br />

remainingSeconds = Math.Abs(latitude) % 3600;<br />

int latitudeMinutes = remainingSeconds / 60;<br />

int latitudeSeconds = remainingSeconds % 60;<br />

return String.Format("{0}˚{1}'{2}\"{3},{4}˚{5}'{6}\"{7}",<br />

longitudeDegrees, longitudeMinutes, longitudeSeconds,<br />

northSouth, latitudeDegrees, latitudeMinutes,<br />

latitudeSeconds, eastWest);<br />

Conversion from a string happens with the Parse() method. The string that is entered from the user<br />

is represented in the SqlString parameter of the static method Parse(). First, the Parse() method<br />

checks if the string represents a null value, in which case the Null property is invoked to return an empty<br />

SqlCoordinate object. If the SqlString s does not represent a null value, the text of the string is<br />

converted to pass the longitude <strong>and</strong> latitude values to the SqlCoordinate constructor:<br />

public static SqlCoordinate Parse(SqlString s)<br />

{<br />

if (s.IsNull)<br />

return SqlCoordinate.Null;<br />

try<br />

{<br />

string[] coordinates = s.Value.Split(',');<br />

char[] separators = { '˚', '\'', '\"' };<br />

string[] longitudeVals = coordinates[0].Split(separators);<br />

string[] latitudeVals = coordinates[1].Split(separators);<br />

if (longitudeVals.Length != 4 && latitudeVals.Length != 4)<br />

throw new ArgumentException(<br />

"Argument has a wrong syntax. " +<br />

"This syntax is required: 37˚47\'0\"N,122˚26\'0\"W");<br />

Orientation orientation;<br />

if (longitudeVals[3] == "N" && latitudeVals[3] == "E")<br />

orientation = Orientation.NorthEast;<br />

else if (longitudeVals[3] == "S" && latitudeVals[3] == "W")<br />

orientation = Orientation.SouthWest;<br />

else if (longitudeVals[3] == "S" && latitudeVals[3] == "E")<br />

orientation = Orientation.SouthEast;<br />

else<br />

orientation = Orientation.NorthWest;<br />

return new SqlCoordinate(<br />

int.Parse(longitudeVals[0]), int.Parse(longitudeVals[1]),<br />

int.Parse(longitudeVals[2]),<br />

int.Parse(latitudeVals[0]), int.Parse(latitudeVals[1]),<br />

int.Parse(latitudeVals[2]), orientation);<br />

}<br />

catch (FormatException ex)<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!