That's wasn't the point. I'm stating that True == 1 because that's what's
I've designated.
Nevertheless, I like your response.
Thank you.
On 8/3/06, Sébastien Lorion <[EMAIL PROTECTED]> wrote:
bool b1 = (FixDBNull<int>.Convert(dr["myIntColumn1"], 0) == 1);
bool b2 = (FixDBNull<int>.Convert(dr["myIntColumn1"], 0) == -1);
bool b3 = (FixDBNull<string>.Convert(dr["myStringColumn1"], "N") == "Y");
You cannot assume 1 == true, especially with databases.
Sébastien
On 8/3/06, Mike Andrews <[EMAIL PROTECTED]> wrote:
> First, I agree that the whole concept of generics is just to prevent the
> problems as everyone has stated.
> Yet, I have a good reason for wanting such a cast.
>
> Take for example this generic class:
>
> public static class FixDBNull<ItemType> {
>
> /// <summary>
> /// Verifys that the specified data is not DBNULL.
> /// If so, it returns the empty value.
> /// </summary>
> /// <param name="data">The data to verify.</param>
> /// <param name="nullValue">The null value to return if data is
> DBNULL</param>
> /// <returns></returns>
> public static ItemType Convert(object data, ItemType nullValue)
{
> if (data != System.DBNull.Value) {
> return (ItemType)data;
> }else {
> return nullValue;
> }
> }
>
> }
> You use this class/function this way:
>
> ... = FixDBNull<string>.Convert(dr["myStringColumn1"], null);
> ... = FixDBNull<byte[]>.Convert(dr["myByteArrayColumn1"], null);
> ... = FixDBNull<string>.Convert(dr["myStringColumn2"], "myNullValue");
>
> Note: this will fail:
> ... = FixDBNull<bool>.Convert(dr["myIntColumn1"], false);
>
> Notice that I have "(ItemType)data" which means I'm attempting to cast
> whatever "data" is to whatever ItemType is.
> Since I use this stritcly for database manipulations where the value
> returned - data - can be an int and it really represents a bool.
> (I realize this could start another argument regarding the usage of int
vs
> bit for boolean types, but for the purposes of this discussion, let's
just
> assume it's valid).
> Since the compiler will cast object to string (or attempt to do so) and
> object to int, and object to byte[], and object to other intrinsic types
> (except boolean) this generic class/function works quite well for
intrinsic
> data types except for boolean.
> And it's also (for the purposes of this function) desired for an
> InvalidCastException to be raised when the cast is invalid; but when you
> know the type of data your column will return, the cast should always
work.
>
> And since you cannot use the equality (or inequality) operators (=, != )
in
> testing the values to two generic parameters there's no way to determine
if
> the object is a boolean to test a particular value (example: nullValue
will
> never be equal to true or false).
>
> Yes, perhaps some of this can be overcome with Nullable types, so if any
of
> you have any suggestions how I can revise my code to allow for nullable
> types, I'd like to see it.
>
> Thanks,
> Mike
>
>
> On 8/3/06, Chris Anderson <[EMAIL PROTECTED]> wrote:
> >
> > bool x = (value != 0);
> >
> >
> > > -----Original Message-----
> > > From: Discussion of advanced .NET topics.
> > > [mailto:[EMAIL PROTECTED] On Behalf Of Mike
Andrews
> > > Sent: 03 August 2006 14:16
> > > To: [email protected]
> > > Subject: [ADVANCED-DOTNET] Gripe out the C# compiler...
> > >
> > > Guys,
> > >
> > > Can anyone tell me any good reason why the C# compiler will
> > > not allow casting an int to a bool?
> > >
> > > example:
> > >
> > > bool x = (bool)1;
> > >
> > >
> > > instead of doing something like this:
> > > bool x = Convert.ToBoolean(1);
> > > or
> > > bool x = bool.Parse(1);
> > >
> > > Makes it very difficult to write generic methods where I need
> > > to cast a value to a bool.
> > >
> > > Thanks,
> > > Mike
> > >
> > > ===================================
> > > This list is hosted by DevelopMentor(r) http://www.develop.com
> > >
> > > View archives and manage your subscription(s) at
> > > http://discuss.develop.com
> > >
> > >
> > >
> >
> > ===================================
> > This list is hosted by DevelopMentor(r) http://www.develop.com
> >
> > View archives and manage your subscription(s) at
> > http://discuss.develop.com
> >
>
> ===================================
> This list is hosted by DevelopMentor(r) http://www.develop.com
>
> View archives and manage your subscription(s) at
http://discuss.develop.com
>
--
Sébastien Lorion
Software Architect / Architecte organique
[EMAIL PROTECTED]
===================================
This list is hosted by DevelopMentor(r) http://www.develop.com
View archives and manage your subscription(s) at
http://discuss.develop.com
===================================
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com