-4

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. :'(

user3204773
  • 11
  • 1
  • 4
  • possible duplicate of [What's the difference between String and string?](http://stackoverflow.com/questions/7074/whats-the-difference-between-string-and-string) – Dozer789 Jan 20 '14 at 23:48
  • 1
    You are getting downvotes most likely because you didn't state what your errors are and there are fundamental issues with the code. Variable initialization mainly. Note: I didn't downvote, just my observation. – Evan L Jan 20 '14 at 23:58

6 Answers6

4

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;
}
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
3

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.

Dozer789
  • 1,980
  • 2
  • 23
  • 43
2

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();
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
2

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 fromstringtoint` 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 ;
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
1

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.

pcnThird
  • 2,362
  • 2
  • 19
  • 33
1

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

Omidam81
  • 1,887
  • 12
  • 20