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;   

2 Answers 2

5
int nextCode = teacherManagerObj.GetCode();
codeTextBox.Text = nextCode.ToString();
Sign up to request clarification or add additional context in comments.

2 Comments

'nextCode' is already an 'int' type. Then why convert it again in 'int'??? this code is not working bro.
@Ikr refresh the page. I was writing too fast. I changed my answer. :)
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.