I have this code in c#:
int i;
i += textBox1.Text;
textBox1.Text = i;
But it is giving me errors on both of those last codes. How do I fix it? What am I doing wrong?
Why is this question getting down votes? It is a perfectly fine question. :'(
I have this code in c#:
int i;
i += textBox1.Text;
textBox1.Text = i;
But it is giving me errors on both of those last codes. How do I fix it? What am I doing wrong?
Why is this question getting down votes? It is a perfectly fine question. :'(
try this:
i+= Convert.ToInt32(textBox1.Text);
textBox1.Text = i.ToString();
There are other functions in Convert
class as well like ToInt64
, ToDouble
etc. Also you may want to use TryParse
if you are not sure the text box always contains number:
var x = 0;
if(int.TryParse(textbox1.Text, out x))
{
i+=x;
}
You are trying make a String
an Int
You have to Convert
the String
to an Int
Put this code in:
i = Convert.ToInt32(textBox1.Text);
textBox1.Text = i.ToString();
You can also Parse
it, and do TryParse
.
If you want to do this for something besides a int, you can change to whatever you want. For example:
Convert.ToDouble
Converts it to a Double
, instead of a Int
.
If you try to use +=
with an uninitialized variable,you will get a compiler error:
The variable i might not be initialized before accessing.
So first you need to initialize your variable, simply assign it to 0 when you defining it:
int i = 0;
And then you are trying to add a string to an integer.C# is a strongly typed language.Therefore you can't assign an integer to a string directly.To do this first you should Convert your string to Integer, then append to your actual variable:
int i += Convert.ToInt32(textBox1.Text);
Third line you are trying to assign an integer to a string.To do this you should convert back to your integer variable to string.You can use .ToString
method or Convert.ToString
textBox1.Text = i.ToString();
Your sample code
int i;
i += textBox1.Text;
textBox1.Text = i;
has some problems.
First, you'll get a compiler error (two, actually) due to the type mismatch. i
is an int
; textBox
.Textis a string. There is no implicit cast or conversion from
stringto
int` and so the compiler throws an error.
Second, having fixed the above issue, you'll get another compiler error due to i
not being initialized. The C# specification required variable to be explicitly assigned a value prior to first use. Something like i += j;
is shorthand for i = i + j;
. And since you haven't assigned a value to i
, the compiler throws an error.
You might note that the compiler errors you see
error CS0030: Cannot convert type 'string' to 'int'
error CS0029: Cannot implicitly convert type 'string' to 'int'
and
error CS0165: Use of unassigned local variable 'i'
are rather descriptive of the problem. The solution should be rather obvious on reading the compiler error messages. What you want to do is something like:
int value ;
bool parsed = int.TryParse(textBox1.Text,out value ) ;
if ( !parsed ) throw new InvalidOperationException( "you should have client-side validators on textBox1") ;
i += value ;
You can't add a string to an int
unless you parse it and it succeeds. You may also want to call .ToString()
on i
when assigning it to the textbox's text. Consider using int.TryParse()
, by the way, or at least handle invalid input.
you can also try this one;
int xout;
if(int.TryParse(textbox1.Text, out xout)){
i += xout;
textbox1.Text = i.ToString();
}
the different between Convert.ToInt32 and int.TryParse: if textbox have not integer string Convert.ToInt32 throw and exception but int.TryPary will not