casting bit to boolean

J

Jeff

Hey

..net 2.0

in my c# code I retrieve a value from a column in database (sql server 2000)
(using SqlReader) this database column has the datatype Bit!

Now I'm looking a way to cast this bit value into an .Net boolean variable

Boolean test = (Boolean) _SqlReader["modified"]; //this throws an
exception - specified cast is not valid!

So I was thinking maybe this could do it:
Boolean test = false;
int value = (int) _SqlReader["modified"];
if (value == 1)
test = true

I wonder if there is a much better of doing this cast?

any suggestions?
 
C

Chris Shepherd

Jeff said:
So I was thinking maybe this could do it:
Boolean test = false;
int value = (int) _SqlReader["modified"];
if (value == 1)
test = true

I wonder if there is a much better of doing this cast?

any suggestions?

Will Convert.ToBoolean((int) _SqlReader["modified"]) work?


Chris.
 
N

Nicholas Paldino [.NET/C# MVP]

Jeff,

Something tells me that "modified" might not actually be a bit data
type. If it was, then the provider (the classes in the
System.Data.SqlClient namespace) should have converted it for you.

If you put _SqlReader["modified"] in the debugger, what is the type that
it says it returns?
 
M

Michael Rubinstein

Jeff, if you mean SqlDataReader as in
System.Data.SqlClient.SqlDataReader
you could call

if (_SqlReader.GetBoolean(_SqlReader.GetOrdinal("modified")))
{
}

Michael
 
B

Ben Voigt [C++ MVP]

Jeff said:
Hey

.net 2.0

in my c# code I retrieve a value from a column in database (sql server
2000) (using SqlReader) this database column has the datatype Bit!

Now I'm looking a way to cast this bit value into an .Net boolean variable

Boolean test = (Boolean) _SqlReader["modified"]; //this throws an
exception - specified cast is not valid!

So I was thinking maybe this could do it:
Boolean test = false;
int value = (int) _SqlReader["modified"];
if (value == 1)
test = true

I wonder if there is a much better of doing this cast?

How about:

bool test = (_SqlReader["modified"] == 1);
 
W

Wingot

-----Original Message-----
From: Ben Voigt [C++ MVP] [mailto:[email protected]]
Posted At: Thursday, 6 December 2007 7:01 AM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: casting bit to boolean
Subject: Re: casting bit to boolean


Jeff said:
Hey

.net 2.0

in my c# code I retrieve a value from a column in database (sql server
2000) (using SqlReader) this database column has the datatype Bit!

Now I'm looking a way to cast this bit value into an .Net boolean variable

Boolean test = (Boolean) _SqlReader["modified"]; //this throws an
exception - specified cast is not valid!

So I was thinking maybe this could do it:
Boolean test = false;
int value = (int) _SqlReader["modified"];
if (value == 1)
test = true

I wonder if there is a much better of doing this cast?

How about:

bool test = (_SqlReader["modified"] == 1);
any suggestions?

I was going to suggest the same, but it was going to be a
if..then..else. Good job finding the one liner that did it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top