Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 736
  • Last Modified:

Comparint two _bstr_t

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
0
turn123
Asked:
turn123
  • 2
  • 2
3 Solutions
 
ZoppoCommented:
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?

ZOPPO
0
 
turn123Author Commented:
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.CreateInstance(__uuidof(Recordset));
	boxRecordset->Open(query.c_str(), _variant_t((IDispatch*)m_conn, true), adOpenForwardOnly, adLockReadOnly, adCmdText); 

	boxRecordset->MoveFirst();
	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;

		m_allBoxes.push_back(newBox);
			
		if(debug)
		{
			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;
		}

		boxRecordset->MoveNext();
	}
	boxRecordset->Close();
}

Open in new window


Thank you
0
 
sarabandeCommented:
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++).

Sara
0
 
ZoppoCommented:
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.

ZOPPO
0
 
turn123Author Commented:
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.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now