1

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.

ICoffeeConsumer
  • 882
  • 1
  • 8
  • 22

1 Answers1

1

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

MessageBox.Text = SenderBox.Text;
SenderBox.Text = "cleared";
CrazyCasta
  • 26,917
  • 4
  • 45
  • 72
  • Because it doesn't work for the same reason my code doesn't work. Someone already proposed this then revoked their answer... thanks for trying though. – ICoffeeConsumer Nov 04 '12 at 03:07
  • 1
    Well, I would suggest `MessageBox.Text = string.Copy(SenderBox.Text);` with the caveat that if this works it is a framework bug since they have specified that strings are supposed to be immutable. – CrazyCasta Nov 04 '12 at 03:12