Did not see your whole point type, but does not seem like your implementing
it right in terms of isNull bool. You may also want to concider a
non-mutable Point. So you can't change x or y after construction. You just
create new point instances (ala String, SqlDateTime, etc).
--Test
declare @p Point
set @p = Point::GetNewPoint(1,2)
select @p.ToString()
set @p.Y = 3
select @p.ToString()
-- Point UDT
using System;
using System.Data.Sql;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
[Serializable]
[Microsoft.SqlServer.Server.SqlUserDefinedType(Format.Native, IsByteOrdered
= true)]
public struct Point : INullable
{
private int x;
private int y;
private bool isNull;
public Point(int x, int y)
{
this.x = x;
this.y = y;
this.isNull = false;
}
public static Point GetNewPoint(int x, int y)
{
return new Point(x, y);
}
public Int32 X
{
get
{
if (IsNull)
throw new SqlNullValueException();
return this.x;
}
[SqlMethod(IsMutator = true)]
set
{
if ( IsNull )
throw new SqlNullValueException();
this.x = value;
}
}
public Int32 Y
{
get
{
if (IsNull)
throw new SqlNullValueException();
return this.y;
}
[SqlMethod(IsMutator = true)]
set
{
if (IsNull)
throw new SqlNullValueException();
this.y = value;
}
}
public bool IsNull
{
get
{
return (this.isNull);
}
}
public static Point Null
{
get
{
Point pt = new Point();
pt.isNull = true;
return pt;
}
}
public override string ToString()
{
if (this.IsNull)
return "NULL";
return x + "," + y;
}
[SqlMethod(OnNullCall = false)]
public static Point Parse(SqlString s)
{
if (s.IsNull)
return Point.Null;
string[] xy = s.Value.Split(",".ToCharArray());
int x = Int32.Parse(xy[0]);
int y = Int32.Parse(xy[1]);
return new Point(x, y);
}
}
--
William Stacey [MVP]
|