Frage

I have a pretty decent background in Java and C++, but I'm struggling to do something trivial in C#. I have two TextBoxes: MessageBox and SenderBox. I want to send the text from SenderBox to MessageBox (which is easy), but I want to clear SenderBox once that is done. The following is basically the code that is fired when you press Enter to send the text:

string temp = SenderBox.Text;
SenderBox.Text = "cleared";
MessageBox.Text = temp;

Most programming languages are somewhat procedural or at least execute in some orderly way. Why does C#/WPF for Windows 8 apps seem to defy this standard? You would expect temp to be set equal to the value of SenderBox, first of all. If you look at the code, temp should not equal "cleared" unless that's what SenderBox contains. At that point (line 1), it doesn't. I tried to create a send(msg) function to dereference the weird string, but that didn't change a thing. The following code runs as expected:

string x = "abc";
string y = x;
y = "123";
MessageBox.Text = x;

Can someone enlighten me? Not sure what's going on here.

War es hilfreich?

Lösung

If you're not trying to switch the contents of the two boxes, why not just do:

MessageBox.Text = SenderBox.Text;
SenderBox.Text = "cleared";
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top