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 3 1436
try
result = (surrogate.ToString() == Value.ToString()) ? DBNull.Value : Value;
object to object comparison is causing the problem for you ...
"Andy" <aj********@capcitypress.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com... 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
Why not
result = surrogate.Equals(Value) ? DBNull.Value : Value;
?
For objects, == is an identity comparison: it compares pointers. The
Equals() method compares the values of the objects where it is
overloaded to do so for particular types.
Ahh.. what was throwing me was that if i went to the immediate window
or did a quick watch on the expression, it was always evaluating to
true.
I'll try the Equals method.. thanks!
Andy This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jim Jewett |
last post by:
Normally, I expect a subclass to act in a manner consistent
with its Base classes. In particular, I don't expect to
*lose* any functionality, unless that was the whole point of
the subclass. ...
|
by: OtisUsenet |
last post by:
Hello,
I am trying to call Javascript from FORM's onSubmit, and return false,
so the form is not actually submitted.
Normally I can just add 'return false;' to onSubmit to accomplish
this, like...
|
by: Simone |
last post by:
Hello All.
I would like some advice.
What is the best way to make an Access database design not accessible?
Like accessing tables, form design and etc.
Is it a good idea to make a MDE file?...
|
by: Tatu Portin |
last post by:
Is FALSE == 0?
In other words, does 'function ()' execute on all platforms:
if ( (1 == 2) == 0 )
function ();
if ( (7!) == 0 )
function ();
I would like if you could point to some...
|
by: Paminu |
last post by:
As I remember if(1) evaluates to true and all other numbers including 0
evaluate to false.
But where do I find out about this for sure?? I have looked through K&R, all
the C for dummies books...
|
by: Tim::.. |
last post by:
Can someone tell me why the value of my Boolean is always false!
Even after I have declared that my variable iNews should equal TRUE it seems
to remain false...
Can some please explain why...
...
|
by: Steve |
last post by:
After running into a design wall with my traditional thread approach, I
realized that a BackgroundWorker object would fit my needs pretty good.
Never really used them before.
In a nutshell, my...
|
by: bonk |
last post by:
I have a c# project as part of a larger VS 2005 solution that always
gets build optimized and I therefore can not evaluate any values while
debugging through the code ("Cannot evaluate expression...
|
by: prashanth023 |
last post by:
Hi, This is prashanth kumar.
I am getting error , i am gettign number of records using ajax. Please solve this
if n >0 i am putting return false in response function.
but form is going to...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |