Click to See Complete Forum and Search --> : incrementing /decrementing a value in a textbox
Programmer127
January 24th, 2007, 05:50 PM
How can I get the value of the number in a textbox as an integer, manipulate that value, and convert it back to a System::String^ to put back into the textbox?
I tried the following, but I know it is wrong somehow.
int x = (int)this->label1->Text + 1;
if anyone can point me in the right direction, I would really appreciate it. Thanks in advance.
wuhoo
January 24th, 2007, 06:03 PM
Id pull it out as a String
Put it into a stringstream
and let the stingstream do the work for you
std::string str = "77";
std::stringstream ss;
ss << str;
int number = 0;
ss >> number;
cilu
January 25th, 2007, 02:02 AM
[ redirected ]
cilu
January 25th, 2007, 02:06 AM
What you did with converting a String (Text property) to int, does not work.
Do this:
// to increment
int value = Convert::ToInt32(label1->Text) + 1;
label1->Text = value.ToString();
Programmer127
January 25th, 2007, 07:06 PM
Thanks a lot for the help. I am a beginner, so I am sure it was an obvious question. I appreciate it!
Programmer127
January 25th, 2007, 07:15 PM
Sorry- it didn't work. I don't know if I am doing something wrong, but I got the following error:
error C2440: '=' : cannot convert from 'overloaded-function' to 'overloaded-function'
I think I got the code right:
--------------------------------------------------------------------------------
// to increment
int value = Convert::ToInt32(label1->Text) + 1;
label1->Text = value.ToString;
--------------------------------------------------------------------------------
next time I will test the code before I respond, sorry! Thanks for any help!
cilu
January 26th, 2007, 03:02 AM
So, does it work or not? It's now unclear to me.
James2007
January 27th, 2007, 02:48 PM
int nValue = System::Integer::Parse(TextBoxName->Text);
++nValue;
TextBoxName->Text = nValue.ToString();
kaixa
January 28th, 2007, 12:28 PM
try this
Int32 i = System::Convert::ToInt32(label1->Text) ;
++i;
textbox1->Text = i.ToString();
cilu
January 29th, 2007, 04:25 AM
James and Kaiax, may I know what's different in your samples from what I already posted above?
James2007
January 29th, 2007, 07:38 AM
The 'Integer' type will always be around in the .NET runtime. It's a core part of the language. (DUH)
So it's safe to assume Integer::Parse will be around as long as .NET exists. Convert::ToInt32 might not exist for the life of .NET :>)
For 'functional' purposes, the two ways listed end up with the same result however.
Integer::Parse is better design in my opinion as well, because if you need to convert a string to an int, it's more convenient first of all. Second of all, if you are familiar with the term 'black boxing', which simply means only exposing the methods to the client (user) that you want them to use..... Parse is presented as a static method of the System::Integer type. It converts a string to an int value. It makes sense to expose this method. Now, when going through 'class libraries' where is the first place you are going to look for a method dealing with a type?
That's right.... The type itself.
And for personal preference, I would use Integer::Parse as well.
Because I think of a string as just that.... TEXT.... It's not an integer, it's not anything else. It's text.
When I think of 'conversion' I think of something you can static_cast with no problem. And int and a string are two entirely different entities. Thinking of it like that, you should have to PARSE a string's contents to get an INTEGER value. So, Integer::Parse makes much more sense to me.
It's like this....
Can a string be made of entirely of digit values so it can be 'converted' to an integer? YES....
But is a string always a 'string' of chars that are all digits? NO....
So a string could be an int, but it isn't always....
An INTEGER can always be converted to a string, but a STRING cannot always be converted to an integer.
It's the ARSE BACKWARDS building blocks of computer programming....
An integer type is one of the building blocks of everything else. (So in a sense, it's considered a base....)
String could be considered 'derived' because it takes the element integer and makes use of it, and other things....
BUT....
A derived class can (implemented correctedly) easily represent (or convert if not perfectly 'is a') it's base class.
However, in this way of thinking this isn't true. (Sure, String is NOT derived from integer, but I think you know what my way of thinking is here.)
And INTEGER which could be considered the base, can always be converted to a string type. (When a base class should not know anything about it's derivatives and/or FUTURE derivatives.)
codeguru.com
Copyright 2007 Jupitermedia Corporation All Rights Reserved.