How to convert a 'string' to

Posted on 2006-07-12
Last Modified: 2012-06-22

I'm trying to convert a __gc string captured from a textBox to a string from the 'string' class.  Here's what I'm doing now but I think it is problematic:

char TxtBuff[15];
string TxtBox;

TxtBox = TxtBuff;

sprintf has been doing the job but I think it is creating 'unseen' problems because I'll get errors during runtime that are weird to say the least.

What is the correct way ?  Thanks

Question by:John500
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
LVL 15

Expert Comment

ID: 17092613
Use StringToHGlobalAnsi

const char* chars =
      (const char*)(Marshal::StringToHGlobalAnsi(str)).ToPointer();
std::string cString = chars;


Author Comment

ID: 17093909
I was able to follow most of your code but the last line threw me.  Can you rewrite it using the actual objects I am.  That is:

textBox1->Text         //  actual string being captured

string TxtBoxValue;   // actual desired end result or variable

Is this the idea :

const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(textBox1->Text)).ToPointer();
string TxtBoxValue =  chars;

Concerning this line:


What does it do ?  If 'chars' can be assigned to 'TxtBoxValue', why go any further ?

Author Comment

ID: 17095130
If it's any easier, I could accomplish the same objective by doing a comparison if I knew how.   Using the 'string' class I would do something like this:

string MyString = "abcdefg";

if(MyString.Compare(0,7,"abcdefg") == 0)

but how to with the textBox ?


Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 15

Expert Comment

ID: 17095820
You can do this,
if(String::Compare(textBox1->Text,"abcdefg") == 0)

Author Comment

ID: 17100202

I ended up needing both approaches.  I've boosted the points to get additional info here.  The operations I'm handling capture input from one groupBox, perform logic and update another groupBox.  The logic requires the conversions we talked about and seem to be fine from the standpoint of compiling...

I haven't had a chance to run the program because when it comes to updating the othe groupBox I've determined I need to use an array to store values.  Here's what I'm doing in Form1.h:

     __gc System::String *groupBox1array;

private: System::Void Form1_Load(System::Object * sender, System::EventArgs * e)
                    groupBox1array = __gc new System::String[5];   //  one index for each groupBox element


When I compile I get the following error on the line below:

if(TextBox1_Value == 16)     // TextBox1_Value first converted to a 'string' and then a 'long'
        groupBox1array[1] = "This Is A Test";     // ERROR HERE

error C2845: '[': cannot perform pointer arithmetic on __gc pointer 'System::String __gc *'

 __gc System::String *groupBox1array;   //  ERROR HERE

error C3150: 'System': '__gc' can only be applied to a class, struct, interface or pointer

How do I get this __gc String array to compile and would that take care of the first error ?


LVL 15

Accepted Solution

lakshman_ce earned 500 total points
ID: 17100481
Declare string array as

System::String *groupBox1array[];

In the load method allocate memory as

groupBox1array = new String*[5];

You can check the length directly on String object like this

if((textBox1->Text)->Length == 16)
groupBox1array[1] = "This Is A Test";


Author Comment

ID: 17104820
Thanks again !

Please keep an eye out for another question over the next 24 hours.  I don't know whether I have a threading issue or not.  I have  one main thread instead of multiple threads.  This thread polls five sockets which all need to access a queue.

Its the same queue you checked out for me a few weeks back.........


Author Comment

ID: 17121456

Can you check out this question and give me some input if you have any?   Maybe something will jump out at you in terms of why data is not being held in my queue.  



Author Comment

ID: 17122701

I added another comment which shows the function responsible for updating the queue.


How about taking a look?  Thanks,


Author Comment

ID: 17129829

Can you check out my last post on the question below?  Sorry to be a pest and thank you very much.


Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
In an interesting question ( here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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