#define compileErrorsNot

using System;
using System.Data;
using System.Data.SqlTypes;


namespace Chapter3
{

	public class Nulls
	{
		public static SqlInt32 Same1(SqlInt32 a)
		{
			// runtime error waiting happen
			// if a is null this will produce a
			// runtime exception, which in turn
			// will raise and error from the
			// function or procedure in which
			int i = a.Value;
			return i;
		}
		public static SqlInt32 Same2(SqlInt32 a)
		{
			// this properly tests for null
			// before attempting to extract value
			if(a.IsNull)
			{
				return SqlInt32.Null;
			}
			int i = a.Value;
			return i;
		}
	}

}

namespace Math
{
	public partial class Arithmetic 
	{
		public static int AddTheseInt(int a1, int a2)
		{
			return a1 + a2; 
		}
		public static int Length(byte[] bytes)
		{
			return bytes.Length; 
		}
	}
}

namespace Math
{
	public partial class Arithmetic 
	{
		public static SqlInt32 AddThese(SqlInt32 a1, SqlInt32 a2)
		{	
			return a1 + a2; 
		}
	}
}

namespace PlanView
{
	public class Limits 
	{
		public static int Edge(SqlDouble d1, SqlInt32 i2)
		{	
			// gets SqlDouble value as CLR double
			double d = d1.Value;
			if(System.Math.Sqrt(d) > 3.0)
			{
				return 7;
			}
			int i = i2.Value;
			return i;
		}
	}
}
namespace Math
{
	public partial class Arithmetic
	{
		public static int SameAs1(SqlInt64 i)
		{
			// this compiles and works as expected
			return (int)i;
		}
#if compileErrors
		public static int SameAs2(SqlInt64 i)
		{
			// this produces compile time error
			return i.Value;
		}
#endif		
		
	}
}
namespace Math
{
	public partial class Arithmetic
	{
		public static SqlBoolean IsTrue(SqlBoolean g)
		{
			// this compiles and works as expected
			return g;
		}

	}
}
namespace Math
{
	public partial class Arithmetic
	{
		public static SqlDouble MixedOp1(SqlDouble d, SqlSingle s)
		{
			// this compiles and works as expected
			return d + s;
		}
#if compileErrors
		public static SqlSingle MixedOp2(SqlDouble d, SqlSingle s)
		{
			// this produces a compile time error because
			// an SqlDouble cannot be implicitly coerced
			// to an SqlSingle
			return d + s;
		}
#endif
	}
}
namespace Math
{
	public partial class Arithmetic
	{
		public static int TimeCompare(SqlTime t, SqlBinary s)
		{
			// this compiles and works as expected
			return t.CompareTo(s);
		}
	}
}



