Is there a way to store 2 wstring values to each list item of a comboBox in a COM project?

Hi, I read in a name field and a description field for each list item of a comboBox on my Dialog page.  I want to display only the name in the Combobox but I want to be able to get its coorsponding despcritpion in the code.  Do I need to buid an array and deal with that each time my code need the descrption field of a selected name in the comboBox or is there a way to store it and access it easiy from the comboBox.  I know C# .net 's comboBox offers this feature but what about C++ comboBox?  Thanks.
Who is Participating?
jkrConnect With a Mentor Commented:
>>But I don't understand why I need to use "new" for the wstring pwstr.

If you used a locally declared variable, it would be lost after it went out of scope, since you can only set a data pointer. Thus, you have to allocate the wstring on the heap in order to keep it valid.
You can use CComboBox's SetItemDataPtr/GetItemDataPtr to point to associated data.
lapuccaAuthor Commented:
I'm not using MFC in my project.  Can you show an example or a url for a example how I can do this?  Thank you.
Qithout MFC, that would be like

wstring pwstr = new wstring;

*pwstr = "test";

SendMessage(hComboBox,CB_SETITEMDATA, nItemIndex, (LPARAM) pwstr);

However, you will need to take care of the pointers by deleting them later.
lapuccaAuthor Commented:
Thank you But I don't understand why I need to use "new" for the wstring pwstr.  Doesn't wstring manages it's own memory allocat and de-allocate?  

Oh, Is this becuase the memory is allocated until I'm done with this.  Like I can release this at the end in my desctructor module.  How do I delete a wstring?  So far, I have let it manage its own memory.  

Thank you.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.