Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Basic_string to Listbox problem.

Hi all again, I have been playing around with the basic_string class in C++. Just to see what it can do. It all look pretty good and the functionality is there to perform operations on a string. However i tried to add this string to a listbox, and of course it popped up an error. So I converted it to a Const Char pointer using the c_str() function and added to the listbox, and i got loadds of funny characters in my listbox. Eventually i got it to work by further converting it to a wchar_t.
So could some one please tell is it really necessary to convert a basic_string twice just so i can add it to a listbox without it looking like binary or an encrypted version or something.
[687 byte] By [zeek840] at [2007-11-11 10:26:51]
# 1 Re: Basic_string to Listbox problem.
Hi,

try to use std::string, which is basically a typedef of a basic_string.
You will probably need to convert "\n" to "\n\r" (or "\r\n", I can never remember which way round). Then tuse the data() method of the string object. Works normally for me.

Cheers,
D
drkybelk at 2007-11-11 20:58:55 >
# 2 Re: Basic_string to Listbox problem.
Thanks for that but, i'm not sure i completely understand. Could you provide and example?
zeek840 at 2007-11-11 20:59:55 >
# 3 Re: Basic_string to Listbox problem.
If your application if of Unicode type, then you have to use std::wstring instead (which means std::basic_string< wchar_t >). For example:
std::wstring s = LText;
myListbox.AddString(s.c_str());
You probably are doing this:
std::string s = Text;
myListbox.AddString((wchar_t*)s.c_str());
which is not correct.

Either switch to wstring, or change the type of your application to not use Unicode if not required.

I hope this helps.
Viorel at 2007-11-11 21:00:48 >
# 4 Re: Basic_string to Listbox problem.
Thanks, i've just re-discovered this wonderful thing called 'The Help Files' sorry for the silly questions. As a side note i'd just like to say that i'm more addicited to C++ than any other language i've coded in. It looks practically alien but is quite elegant when you start to use it more.
zeek840 at 2007-11-11 21:01:54 >
# 5 Re: Basic_string to Listbox problem.
As a side note i'd just like to say that i'm more addicited to C++ than any other language i've coded in. It looks practically alien but is quite elegant when you start to use it more.
It gets even better with time, trust me;)
Danny at 2007-11-11 21:02:53 >