15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

User-Defined Types ❘ 959<br />

}<br />

}<br />

// Put your code here<br />

return m_Null;<br />

}<br />

public static SqlCoordinate Null<br />

{<br />

get<br />

{<br />

SqlCoordinate h = new SqlCoordinate();<br />

h.m_Null = true;<br />

return h;<br />

}<br />

}<br />

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

{<br />

if (s.IsNull)<br />

return Null;<br />

SqlCoordinate u = new SqlCoordinate();<br />

// Put your code here<br />

return u;<br />

}<br />

// This is a place-holder method<br />

public string Method1()<br />

{<br />

//Insert method code here<br />

return "Hello";<br />

}<br />

// This is a place-holder static method<br />

public static SqlString Method2()<br />

{<br />

// Insert method code here<br />

return new SqlString("Hello");<br />

}<br />

// This is a placeholder field member<br />

public int var1;<br />

// Private member<br />

private bool m_Null;<br />

Because this type can also be used directly from client code, it is a good idea to add a namespace, which is<br />

not done automatically.<br />

The struct SqlCoordinate implements the interface INullable. The interface INullable is required for<br />

UDTs because database types can also be null. The attribute SqlUserDefinedType is used for automatic<br />

deployment with Visual Studio for UDTs. The argument Format.Native defines the serialization format<br />

to be used. Two serialization formats are possible: Format.Native <strong>and</strong> Format.UserDefined. Format<br />

.Native is the simple serialization format where the engine performs serialization <strong>and</strong> deserialization of<br />

instances. This serialization allows only value types (predefined <strong>and</strong> structs of value types). Reference types<br />

(which include the string type) require the UserDefined serialization format. With the SqlCoordinate<br />

struct, the data types to serialize are of type int <strong>and</strong> bool, which are value types.<br />

Using Format.UserDefined requires the interface IBinarySerialize to be implemented. The<br />

IBinarySerialize interface provides custom implementation for user-defined types. Read() <strong>and</strong> Write()<br />

methods must be implemented for serialization of the data to a BinaryReader <strong>and</strong> a BinaryWriter.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!