Comparint two _bstr_t

Posted on 2012-08-13
Last Modified: 2012-08-14
I feel like I may be missing something obvious here but I have two _bstr_t that I need to compare.

	_bstr_t boxSize = "SMALL";
	_bstr_t smallBox = "SMALL";
	bool smallItems = boxSize == smallBox;

Open in new window

I was expecting smallItems to be true but it is false.  What do I need to do to compare them so smallItems is true in this case but false if the values are not the same?

Thank you
Question by:turn123
    LVL 30

    Accepted Solution

    Hi turn123,

    I just copied these thread lines of code and compiled it, and when I run it smallItems is true after executing the last line. Maybe there's another problem in the code before or after these lines - could you post the complete function?

    LVL 11

    Author Comment

    Hi ZOPPO,

    void BoxManager::loadBoxes()
    	//Variables needed to fill box
    	int productID = 0;
    	_bstr_t boxName;
    	_bstr_t boxSize = "SMALL";
    	_bstr_t smallBox = "SMALL";
    	bool smallItems = boxSize == smallBox;
    	double height = 0;
    	double width = 0;
    	double length = 0;
    	double outsideHeight = 0;
    	double outsideWidth = 0;
    	double outsideLength = 0;
    	double handlingCharge = 0.0;
    	double actualHandlingCharge = 0.0;
    	double weight = 0;
    	bool canBeUsed = false;
    	//Get boxes
    	std::string query = "SELECT ItemCode AS Box_Name, \
    Height, \
    Width, \
    Length, \
    PriceListHeight AS OutsideHeight, \
    PriceListWidth AS OutsideWidth, \
    PriceListLength AS OutsideLength, \
    UnitPrice AS ActualHandlingCharge, \
    SortCode AS HandlingCharge, \
    Weight, \
    RTRIM(UserDefinedField4) AS ItemModelCode \
    FROM dbo.Inventory \
    WHERE Active = 1 \
    AND FreightDataItem = 1";
    	_RecordsetPtr boxRecordset;
    	boxRecordset->Open(query.c_str(), _variant_t((IDispatch*)m_conn, true), adOpenForwardOnly, adLockReadOnly, adCmdText); 
    	while (!(boxRecordset->EndOfFile)) 
    		boxName = boxRecordset->Fields->GetItem("Box_Name")->Value; 
    		VarR8FromDec(&boxRecordset->Fields->GetItem("Height")->Value.decVal, &height);
    		VarR8FromDec(&boxRecordset->Fields->GetItem("Width")->Value.decVal, &width);
    		VarR8FromDec(&boxRecordset->Fields->GetItem("Length")->Value.decVal, &length);
    		VarR8FromDec(&boxRecordset->Fields->GetItem("OutsideHeight")->Value.decVal, &outsideHeight);
    		VarR8FromDec(&boxRecordset->Fields->GetItem("OutsideWidth")->Value.decVal, &outsideWidth);
    		VarR8FromDec(&boxRecordset->Fields->GetItem("OutsideLength")->Value.decVal, &outsideLength);
    		handlingCharge = wcstod(boxRecordset->Fields->GetItem("HandlingCharge")->Value.bstrVal, NULL);
    		VarR8FromDec(&boxRecordset->Fields->GetItem("ActualHandlingCharge")->Value.decVal, &actualHandlingCharge);
    		boxSize = boxRecordset->Fields->GetItem("ItemModelCode")->Value;
    		smallItems = boxSize == smallBox;
    		VarR8FromDec(&boxRecordset->Fields->GetItem("Weight")->Value.decVal, &weight);
    		//Add box to the list
    		box newBox;
    		newBox.m_box_name = boxName;
    		newBox.m_box_name = newBox.m_box_name.erase(newBox.m_box_name.find_last_not_of(" \n\r\t")+1);
    		newBox.m_inside.x = height;
    		newBox.m_inside.y = width;
    		newBox.m_inside.z = length;
    		newBox.m_outside.x = outsideHeight;
    		newBox.m_outside.y = outsideWidth;
    		newBox.m_outside.z = outsideLength;
    		newBox.m_normal = normalizeSize(newBox.m_inside);
    		newBox.m_handling_charge = handlingCharge;
    		newBox.m_actual_handling_charge = actualHandlingCharge;
    		newBox.m_weight = weight;
    		newBox.m_bsmal = smallItems;
    			std::cout << newBox.m_box_name << std::endl;
    			std::cout << newBox.m_inside.x << " " << newBox.m_inside.y << " " << newBox.m_inside.z << std::endl;
    			std::cout << newBox.m_outside.x << " " << newBox.m_outside.y << " " << newBox.m_outside.z << std::endl;
    			std::cout << newBox.m_normal.x << " " << newBox.m_normal.y << " " << newBox.m_normal.z << std::endl;
    			std::cout << newBox.m_handling_charge << std::endl;
    			std::cout << newBox.m_actual_handling_charge << std::endl;
    			std::cout << newBox.m_weight << std::endl;
    			std::cout << boxSize << "\t" << newBox.m_bsmal << std::endl;

    Open in new window

    Thank you
    LVL 32

    Assisted Solution

    to add to Zoppo's comment:

    _bstr_t is a string class and has operator== properly defined in comutil.h

    bool operator==(const _bstr_t& str) const throw();

    Open in new window

    are you using managed c++ (CLR) or unmanaged c++ (WIN32, MFC, ...) ?

    by the way, for easier reading you should do:

    bool smallItems = (boxSize == smallBox);

    Open in new window

    though that should not make any difference (in unmanaged c++).

    LVL 30

    Assisted Solution

    Hm - IMO there's no problem in the code. Is it the first comparsion which fails or the latter one?

    Please try adding a output for the result, i.e. like the last line in this sample:
    	_bstr_t boxName;
    	_bstr_t boxSize = "SMALL";
    	_bstr_t smallBox = "SMALL";
    	bool smallItems = boxSize == smallBox;
    	std::cout << "smallItems = " << smallItems << std::endl;

    Open in new window

    In this sample I see the (expected) output smallItems = 1.

    LVL 11

    Author Comment

    Thank you for your time.

    Now I'm really confused as it is working as expected now and I'm not sure why it wasn't working before.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now