Solved

How to convert a 'string' to

Posted on 2006-07-12
10
270 Views
Last Modified: 2012-06-22
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
Comment
Question by:John500
  • 7
  • 3
10 Comments
 
LVL 15

Expert Comment

by:lakshman_ce
Comment Utility
Use StringToHGlobalAnsi


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

0
 

Author Comment

by:John500
Comment Utility
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
 

Author Comment

by:John500
Comment Utility
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
 
LVL 15

Expert Comment

by:lakshman_ce
Comment Utility
You can do this,
if(String::Compare(textBox1->Text,"abcdefg") == 0)
0
 

Author Comment

by:John500
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 15

Accepted Solution

by:
lakshman_ce earned 500 total points
Comment Utility
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
 

Author Comment

by:John500
Comment Utility
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
 

Author Comment

by:John500
Comment Utility
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
 

Author Comment

by:John500
Comment Utility
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
 

Author Comment

by:John500
Comment Utility
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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now