Improve company productivity with a Business Account.Sign Up

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

How to convert a 'string' to

Hi,

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;

sprintf(TxtBuff,"%s",textBox1->Text);
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

0
John500
Asked:
John500
  • 7
  • 3
1 Solution
 
lakshman_ceCommented:
Use StringToHGlobalAnsi


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

0
 
John500Author Commented:
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:

Marshal::FreeHGlobal(IntPtr((void*)chars));

What does it do ?  If 'chars' can be assigned to 'TxtBoxValue', why go any further ?
0
 
John500Author Commented:
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 ?

textBox->text

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
lakshman_ceCommented:
You can do this,
if(String::Compare(textBox1->Text,"abcdefg") == 0)
0
 
John500Author Commented:
lakshman_ce,

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:

private:
     __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 ?

Thanks




0
 
lakshman_ceCommented:
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";

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

John
0
 
John500Author Commented:
lakshman_ce,

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.  

Thanks

http://Q_21920098.html
0
 
John500Author Commented:
lakshman_ce,

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

http://Q_21920098.html

How about taking a look?  Thanks,

John
0
 
John500Author Commented:
lakshman_ce,

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

http://Q_21920098.html
0
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.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

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