Comparing Object* and int

  • Thread starter Thread starter Tim
  • Start date Start date
T

Tim

Hi,
I am trying to do a simple comparison and finding it
quite difficult. I call a SQL Server stored procedure
which returns a Bit as one of the record fields. All I
want to do is see if the Bit being read is a 1. I have
tried this:

if(dbDataReader->get_Item("On") == 1)

but this does not work since get_Item returns an Object*.
So, I searched around some and found the __box function:

if(dbDataReader->get_Item("InGame")->Equals(__box(1)))

This also did not work, it read false everytime. I have
tried casting to an Int32, but it gives me an error. I
can not seem to find an answer to how to do this simple
comparison anywhere. Does anyone know how?

Thank you,

Tim
 
I ended up using the System::Convert function to convert
the Object* to an Int32. This seemed to work.

-Tim
 
Try the following:

Assuming that the value of column "On" is never NULL:

int onOrdinal = dbDataReader->GetOrdinal(S"On");
bool onValue = dbDataReader->GetBoolean(onOrdinal);

If "On" might be NULL:

int onOrdinal = dbDataReader->GetOrdinal(S"On");
SqlBoolean onValue = dbDataReader->GetSqlBoolean(onOrdinal);
if (onValue.get_IsNull()) {
// ...
} else {
bool isTrue = onValue.get_IsTrue();
// ...
}

If you know the SQL data type of a column, it is usually better to use
one of the statically typed GetXXX methods instead of the get_Item method.
Hi,
I am trying to do a simple comparison and finding it
quite difficult. I call a SQL Server stored procedure
which returns a Bit as one of the record fields. All I
want to do is see if the Bit being read is a 1. I have
tried this:

if(dbDataReader->get_Item("On") == 1)

but this does not work since get_Item returns an Object*.
So, I searched around some and found the __box function:

if(dbDataReader->get_Item("InGame")->Equals(__box(1)))

This also did not work, it read false everytime. I have
tried casting to an Int32, but it gives me an error. I
can not seem to find an answer to how to do this simple
comparison anywhere. Does anyone know how?

The reason why this fails is that get_Item returns a
System::Data::SqlTypes::SqlBoolean instance for a Bit column.

So, the following should work:

if(dbDataReader->get_Item("InGame")->Equals(__box(SqlBoolean::True)))

but this is a bad way to do it compared to

if(dbDataReader->GetBoolean(dbDataReader->GetOrdinal(S"InGame")))

since the former is not statically typed and allocates two objects on
the heap, whereas the latter is statically typed and does not perform
any allocation.
 
Back
Top