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.

User-Defined Types ❘ 961<br />

With the variables longitude <strong>and</strong> latitude, the longitude <strong>and</strong> latitude values are stored using seconds.<br />

The constructor with seven integer parameters converts degrees, minutes, <strong>and</strong> seconds to seconds, <strong>and</strong> sets<br />

the longitude <strong>and</strong> latitude to negative values if the coordinate is based in the South or West:<br />

public SqlCoordinate(int longitude, int latitude)<br />

{<br />

isNull = false;<br />

this.longitude = longitude;<br />

this.latitude = latitude;<br />

}<br />

public SqlCoordinate(int longitudeDegrees, int longitudeMinutes,<br />

int longitudeSeconds, int latitudeDegrees, int latitudeMinutes,<br />

int latitudeSeconds, Orientation orientation)<br />

{<br />

isNull = false;<br />

this.longitude = longitudeSeconds + 60 * longitudeMinutes + 3600 *<br />

longitudeDegrees;<br />

this.latitude = latitudeSeconds + 60 * latitudeMinutes + 3600 *<br />

latitudeDegrees;<br />

switch (orientation)<br />

{<br />

case Orientation.SouthWest:<br />

longitude = -longitude;<br />

latitude = -latitude;<br />

break;<br />

case Orientation.SouthEast:<br />

longitude = -longitude;<br />

break;<br />

case Orientation.NorthWest:<br />

latitude = -latitude;<br />

break;<br />

}<br />

}<br />

The INullable interface defines the property IsNull, which must be implemented to support nullability.<br />

The static property Null is used to create an object that represents a null value. In the get accessor a<br />

SqlCoordinate object is created, <strong>and</strong> the isNull field is set to true:<br />

public bool IsNull<br />

{<br />

get<br />

{<br />

return isNull;<br />

}<br />

}<br />

public static SqlCoordinate Null<br />

{<br />

get<br />

{<br />

return new SqlCoordinate { isNull = true };<br />

}<br />

}<br />

A UDT must be converted from <strong>and</strong> to a string. For conversion to a string, the ToString() method of the<br />

Object class must be overridden. The variables longitude <strong>and</strong> latitude are converted in the following<br />

code for a string representation to show the degrees, minutes, <strong>and</strong> seconds notation:<br />

public override string ToString()<br />

{<br />

if (this.isNull)<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!