Browse DevX
Sign up for e-mail newsletters from DevX


Creating User-Defined Data Types in Yukon : Page 2

One key feature of CLR support is the ability to create User Defined Data Types (UDT). This article demonstrates how to create your own UDT using Yukon and the Whidbey-release of Visual Studio .NET.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Implement the Object Interface
After decorating the class, you need to implement the .NET Framework INullable Interface. Interfaces are implemented using the Implements statement. The following snippet demonstrates how to implement the INullable interface.

Public ReadOnly Property _ IsNull() As Boolean Implements _ System.Data.SqlTypes_ .INullable.IsNull Get Return Me._IsNull End Get End Property

After implementing the INullable interface, you are required to implement the ToString() and Parse() methods and the Null() property

The ToString method is a common function found on most .NET classes. This function returns a representation of your data type as a string. Remember when you return the value from the ToString function you need to include a representation of NULL values. The following snippet shows the ToString method for this article's example.

Public Overrides Function_ ToString() As String If Me.IsNull Then Return "NULL" Else Return Me._cInternalValue End If End Function

The other method that needs attention is the Parse() method. This is the method that SQL Server calls whenever someone inserts or updates data defined as your data type. The API for this method is as follows:

Parse(cData as SqlString) _ as <YourType>

As you can see, this function receives a parameter declared as type SqlString. Upon receiving this code, you perform a number of tasks. The first task is to determine whether or not the data passed in is NULL. If it is, return Nothing from this method.

The second task is to convert the passed-in data to a .NET data type. This is done with the Convert class. After converting the passed-in data to a .NET data type, you can validate the data. If the data passed in does not conform to your rules, you can throw an exception. Figure 1 shows what an error thrown from a custom data type looks like.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date