469,631 Members | 995 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

system::string to int with VC++

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)

Apr 6 '06 #1
11 11485
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.
br**********************@hotmail.com
Remove only "_nos_pam"
Apr 6 '06 #2
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);
--
Tomas Restrepo
to****@mvps.org
http://www.winterdom.com/
Apr 6 '06 #3
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 ^'

Apr 6 '06 #4
> 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.
br**********************@hotmail.com
Remove only "_nos_pam"

Apr 6 '06 #5
(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)

Apr 6 '06 #6
(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.
br**********************@hotmail.com
Remove only "_nos_pam"
Apr 6 '06 #7
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.

Apr 7 '06 #8
it's good!! thanks!! i get this error, because all textbox must be
filled!!
;)

Apr 7 '06 #9
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 ^'

Apr 7 '06 #10
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

Apr 7 '06 #11
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 ;)

Apr 7 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jay | last post: by
15 posts views Thread by Yifan | last post: by
24 posts views Thread by Marcus Kwok | last post: by
14 posts views Thread by =?Utf-8?B?Sm9hY2hpbQ==?= | last post: by
2 posts views Thread by =?Utf-8?B?QWJoaW1hbnl1IFNpcm9oaQ==?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.