1

I want to get an 'int' value in a variable and set it in a textBox. The 2nd line of this code is showing an error:

This expression cannot be used as an assignment target.

How can I solve it?

int nextCode = teacherManagerObj.GetCode();

//shows error "This expression cannot be used as an assignment target"
Convert.ToInt32(codeTextBox.Text) = nextCode;   
gunr2171
  • 16,104
  • 25
  • 61
  • 88
Ikr
  • 43
  • 8

2 Answers2

5
int nextCode = teacherManagerObj.GetCode();
codeTextBox.Text = nextCode.ToString();
Bura Chuhadar
  • 3,653
  • 1
  • 14
  • 17
2

You are trying to convert the Text property of codeTextBox to an Int32 and are trying to assign the Text property with an Int32, while it takes a string and you shouldn't try converting the Text property of TextBox to an Int32, since that isn't possible. You should try to convert the Int32 variable to a string and assign it to the Text property of codeTextBox.

Change

  int nextCode = teacherManagerObj.GetCode();
  Convert.ToInt32(codeTextBox.Text) = nextCode;

To:

codeTextBox.Text = Convert.ToString(nextCode);

Or:

codeTextBox.Text = nextCode.ToString();

The difference between Convert.ToString(nextCode); and nextCode.ToString(), is that the first handles null values. The second one doesn't.

Community
  • 1
  • 1
Max
  • 12,622
  • 16
  • 73
  • 101