using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

[Microsoft.SqlServer.Server.SqlUserDefinedType(Format.UserDefined,
	MaxByteSize=16, IsByteOrdered=true)]
public struct DUDT : INullable, IBinarySerialize
{
	bool isNull;
	decimal value;
#if false
	[SqlFunction]
	public static int TestFunc(out int x)
	{
		x = 1;
		return 0;

	}
#endif
	public override string ToString()
	{
		if (isNull)
		{
			return "NULL";
		}
		// Replace the following code with your code
		return value.ToString();
	}
	[SqlMethod(IsMutator = true)]
	public DUDT AddThis(int amount)
	{
		this.value += amount;
		return this;
	}
	[SqlMethod(IsMutator = true)]
	public DUDT AddThisPlus(int amount, ref int x)
	{
		x = 1;
		this.value += amount;
		return this;
	}

	public int MyIntValue
	{
		get { return (int)decimal.Floor(this.value); }
		set { this.value = value; }
	}
	public bool IsNull
	{
		get
		{
			// Put your code here
			return isNull;
		}
	}

	public static DUDT Null
	{
		get
		{
			DUDT h = new DUDT();
			h.isNull = true;
			return h;
		}
	}

	public static DUDT Parse(SqlString s)
	{
		if (s.IsNull)
			return Null;
		DUDT u = new DUDT();
		u.value = decimal.Parse(s.Value);
		return u;
	}


	#region IBinarySerialize Members

	public void Read(System.IO.BinaryReader r)
	{
		this.value = r.ReadDecimal();
	}

	public void Write(System.IO.BinaryWriter w)
	{
		w.Write(this.value);
	}

	#endregion
}


