system::string to int with VC++

C

Code[R]

hello all, i've a problem :s

i've tried to convert a "system::string" (a textbox) to an "int" but i
have only found a function to convert system::string to char and char
to int :s :

Convert::ToInt32(PtrToStringChars(ligne23->Text));

but i get a bug :s can you help me ? (sorry for my english i'm french)
 
G

Guest

i've tried to convert a "system::string" (a textbox) to an "int" but i
have only found a function to convert system::string to char and char
to int :s :

Convert::ToInt32(PtrToStringChars(ligne23->Text));

It should be no problem to convert a string to an int32 like this:
Convert::ToInt32(ligne23->Text);

Have you tried it, and if so, What happens when you do that? which input do
you supply, and what output value do you get?

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
T

Tomas Restrepo \(MVP\)

hello all, i've a problem :s
i've tried to convert a "system::string" (a textbox) to an "int" but i
have only found a function to convert system::string to char and char
to int :s :

Convert::ToInt32(PtrToStringChars(ligne23->Text));

but i get a bug :s can you help me ? (sorry for my english i'm french)

As Bruno mentioned, the correct way is:

Convert::ToInt32(ligne23->Text);

Furthermore, you should always work with the Convert/ToString/Parse/TryParse
overload that includes a IFormatInfoProvider argument, to ensure your code
works well regarding locales/cultures. So, for example, you could do:

Convert::ToInt32(ligne23->Text, CultureInfo::InvariantCulture);
 
C

Code[R]

thanks, but with this : Convert::ToInt32(ligne23->Text);

i get an error :

Windows::Forms::Control::Text::set(System::String ^)' : cannot convert
parameter 1 from 'int' to 'System::String ^'
 
G

Guest

thanks, but with this : Convert::ToInt32(ligne23->Text);
i get an error :

Windows::Forms::Control::Text::set(System::String ^)' : cannot convert
parameter 1 from 'int' to 'System::String ^'

Could you show some of the surrounding code?
because it seems that it is not the convertsion but something else that is
wrong.
to what are you assigning the resulting converted value?

could it perhaps be something like this:
myControl->Text = Convert::ToInt32(ligne23->Text);

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
C

Code[R]

(i begin in VC++, its not a good code)
......
private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e) {
label2->Text=textBox1->Text;
label13->Text=(ligne13->Text+ligne23->Text+textBox3->Text);
//Not good, it concatene the string
if(ligne11->Text!=""){
if(ligne13->Text==""){
label4->Text="veuillez saisir correctement votre Compta!!\nLigne
1 erronnée";
}
else{
label4->Text="bravo, tu sais ce que c qu'enregistré!!";
}
}
........
(it's a part of an accountancy program)
i need to get the ligne11 (a textBox) in int to make an addition, but,
the type of the textBox is a string, and it concatenate the values :s

system::string isn't a char, then, i can't convert it directly by
Convert::toint32(ligne13)
=> that's why, i get an error (i think :s)
 
B

Bruno van Dooren

(it's a part of an accountancy program)
i need to get the ligne11 (a textBox) in int to make an addition, but,
the type of the textBox is a string, and it concatenate the values :s
system::string isn't a char, then, i can't convert it directly by
Convert::toint32(ligne13)
=> that's why, i get an error (i think :s)

If I understand you correctly (my french isn't that good) you want to make
the sum of some values that are typed into a textbox, and then put that into
a result textbox?

you can do that with something like this:
Int32 sum = Convert::ToInt32(textbox1->Text) +
Convert::ToInt32(textbox2->Text);
textBox3->Text = sum.ToString();

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
C

Code[R]

not in a textbox, in a label, but i just need to change :
mylabel->Text = sum.ToString();
lol i get another error now :p

An unhandled exception of type 'System.FormatException' occurred in
mscorlib.dll

Additional information: Input string was not in a correct format.
 
C

Code[R]

for another code i get :

sum=Convert::ToInt32(ligne13->Text)+Convert::ToInt32(ligne23->Text)+Convert::ToInt32(textBox4->Text);
label13->Text=sum.ToString();
int sum2;
sum2=Convert::ToInt32(ligne14->Text)+Convert::ToInt32(ligne24->Text)+Convert::ToInt32(textBox5->Text);
label15->Text=sum2.ToString();
int sum3=sum-sum2;
label14=sum3.ToString;

sum and sum2 are both integer :s why i get these error ? :

cannot convert from 'overloaded-function' to
'System::Windows::Forms::Label ^'
cannot convert from 'int' to 'System::Windows::Forms::Label ^'
 
C

Code[R]

sorry :s the code is this :
sum=Convert::ToInt32(ligne13->Text)+Convert::ToInt32(ligne23->Text)+Convert::ToInt32(textBox4->Text);
label13->Text=sum.ToString();
int sum2;
sum2=Convert::ToInt32(ligne14->Text)+Convert::ToInt32(ligne24->Text)+Convert::ToInt32(textBox5->Text);
label15->Text=sum2.ToString();
int sum3=sum-sum2;
label14->Text=sum3.ToString;
i've Forget the "->Text" :s
 
C

Code[R]

All ok, i forget the () behind .ToString
(i begin in programmation :) )
Another question, how can i optimize my code? because my program is
50ko, and use more than 15sec. to appear on my screen!!
It is normal for a .Net Application?

Thanks for your help ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top