A
Andy
Hi all,
I'm having a very weird problem. I have the following class..
using System;
namespace CCP.Core.Data {
public class Null {
private Null() {}
private static readonly string stringSurrogate = String.Empty;
private const short numberSurrogate = -1;
private const ushort ushortSurrogate = ushort.MaxValue;
private const uint uintSurrogate = uint.MaxValue;
private const ulong ulongSurrogate = ulong.MaxValue;
public static object GetSurrogate( Type ValueType ) {
object result;
result = null;
switch( ValueType.FullName.ToLower() ) {
case "system.string":
result = stringSurrogate;
break;
case "system.single":
case "system.double":
case "system.int16":
case "system.int32":
case "system.int64":
case "system.decimal":
result = numberSurrogate;
break;
case "system.uint16":
result = ushortSurrogate;
break;
case "system.uint32":
result = uintSurrogate;
break;
case "system.uint64":
result = ulongSurrogate;
break;
default:
result = null;
break;
}
return result;
}
public static object ValueToDatabase( object Value ) {
object result;
object surrogate;
surrogate = GetSurrogate( Value.GetType() );
result = surrogate == Value ? DBNull.Value : Value;
return result;
}
public static object ValueFromDatabase( object Value, Type TargetType
) {
object result;
result = Convert.IsDBNull( Value ) ? GetSurrogate( TargetType ) :
Value;
return result;
}
}
}
ValueFromDatabase works fine, but in my unit tests, ValueToDatabase is
not. Heres a sample test.
Assert.AreSame( DBNull.Value, Null.ValueToDatabase( Null.GetSurrogate(
typeof( short ) ) ), "Failed for short" );
Tracing through, its always returning Value instead of DBNull.Value,
even though I can debug and highlight the surrogate == Value and see
that it evaluates to true, it still picks the false expression.
Any ideas?
Thanks
Andy
I'm having a very weird problem. I have the following class..
using System;
namespace CCP.Core.Data {
public class Null {
private Null() {}
private static readonly string stringSurrogate = String.Empty;
private const short numberSurrogate = -1;
private const ushort ushortSurrogate = ushort.MaxValue;
private const uint uintSurrogate = uint.MaxValue;
private const ulong ulongSurrogate = ulong.MaxValue;
public static object GetSurrogate( Type ValueType ) {
object result;
result = null;
switch( ValueType.FullName.ToLower() ) {
case "system.string":
result = stringSurrogate;
break;
case "system.single":
case "system.double":
case "system.int16":
case "system.int32":
case "system.int64":
case "system.decimal":
result = numberSurrogate;
break;
case "system.uint16":
result = ushortSurrogate;
break;
case "system.uint32":
result = uintSurrogate;
break;
case "system.uint64":
result = ulongSurrogate;
break;
default:
result = null;
break;
}
return result;
}
public static object ValueToDatabase( object Value ) {
object result;
object surrogate;
surrogate = GetSurrogate( Value.GetType() );
result = surrogate == Value ? DBNull.Value : Value;
return result;
}
public static object ValueFromDatabase( object Value, Type TargetType
) {
object result;
result = Convert.IsDBNull( Value ) ? GetSurrogate( TargetType ) :
Value;
return result;
}
}
}
ValueFromDatabase works fine, but in my unit tests, ValueToDatabase is
not. Heres a sample test.
Assert.AreSame( DBNull.Value, Null.ValueToDatabase( Null.GetSurrogate(
typeof( short ) ) ), "Failed for short" );
Tracing through, its always returning Value instead of DBNull.Value,
even though I can debug and highlight the surrogate == Value and see
that it evaluates to true, it still picks the false expression.
Any ideas?
Thanks
Andy