473,378 Members | 1,138 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Comparing Object* and int

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
Nov 17 '05 #1
2 1331
Tim
I ended up using the System::Convert function to convert
the Object* to an Int32. This seemed to work.

-Tim
-----Original Message-----
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
.

Nov 17 '05 #2
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.

Tim wrote:
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.
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Iain | last post by:
Hi Hopefully I am missing something really simple with this question, but here goes. I have two Bitarrays that I would like to compare. At the moment, I am XORing one with the other and...
4
by: - Steve - | last post by:
The following line of vb.net code works fine: if(strAnswer.toUpper() = strUserAnswer.toUpper()) However in an ASP.net page I'm told Object reference not set to an instance of an object ...
5
by: George | last post by:
How do I compare the values of two objects when Option Strict is On? One of the objects is the Tag property from some control (declared as object but holding a value, e.g. an Integer or a String...
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
3
by: Mark Denardo | last post by:
Hi I have an app that has a number of textboxes (richtextboxes to be exact) that get created dynamically and I add a few at a time to panel controls (so I can flip a group in and out when I want). ...
5
by: ma740988 | last post by:
There's a need for me to move around at specified offsets within memory. As as a result - long story short - unsigned char* is the type of choice. At issue: Consider the case ( test code ) where...
5
by: Kermit Piper | last post by:
Hello, I am comparing two date values, one from a database and one that has been converted from a hard-coded string into an actual Date type. So far so good. The problem I'm having is that one...
19
by: Ole Nielsby | last post by:
How does the GetHashCode() of an array object behave? Does it combine the GetHashCode() of its elements, or does it create a sync block for the object? I want to use readonly arrays as...
20
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.