4

Is it possible to change the value stored inside bar after it has been added?

I have tried 'boxing' the string foo but it doesnt work.

string foo = "aaaaaaa";
var bar = new System.Web.UI.HtmlControls.HtmlGenericControl("div") { InnerHtml =foo };
foo = "zzzzzz";
plcBody.Controls.Add(bar);//want this to contain 'zzzzzz'
maxp
  • 24,209
  • 39
  • 123
  • 201
  • 6
    Quick comment, as Nick's answer explains it all other than my picky complaint about wording - this has *nothing* to do with boxing. String is a reference type; boxing only comes into play at all when it comes to value types. – Jon Skeet May 14 '10 at 11:46

1 Answers1

7

To do that you have to set the value, like this:

string foo = "aaaaaaa";
var bar = new System.Web.UI.HtmlControls.HtmlGenericControl("div") { InnerHtml = foo };
bar.InnerHtml = "zzzzzz";
plcBody.Controls.Add(bar);

Strings themselves are immutable (in .NET at least, this isn't universally true), you can't change it after it's been passed...you passed the value of the variable, which is a string reference - you haven't passed a reference to the original variable, so changing the original variable to refer to a different string doesn't do anything. When you change the variable, you're changing which string foo refers to, not editing its original string, as that's immutable.

If it's easier to think of, you're passing "what foo means" not "foo itself", so once that string goes into whatever you're passing it into, it has no relation to the original variable.

David Pine
  • 23,787
  • 10
  • 79
  • 107
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • Hang on - it *is* a reference to the original string object which is passed... but you can't change the contents of the string, precisely because strings are immutable. It's not an alias to the original *variable*, but that's a different matter. It's worth distinguishing between a variable and its value - `foo` is a variable whose value is a string reference; it's not a string (or a string reference) itself. – Jon Skeet May 14 '10 at 11:42
  • Thanks for the reply. When i pass a string as a method parameter, prefixed with the 'ref' keyword it *will* change the original value though, is this some .net trickery? – maxp May 14 '10 at 11:43
  • @maxp - In that case you're passing a reference to the variable, @Jon - I'll try and make it a bit clearer, didn't realize you could read it that way, give me a min – Nick Craver May 14 '10 at 11:44
  • @maxp: "ref" is only effective for the duration of the method call - there's no way of aliasing a variable in a more permanent way. See http://pobox.com/~skeet/csharp/parameters.html – Jon Skeet May 14 '10 at 11:45
  • @nick - setting the property of 'InnerHtml' as a string variable prefixed with 'ref' is not legal, and i take it there is nothing similar? – maxp May 14 '10 at 11:47
  • @Jon - Updated, hopefully that's a bit clearer, feel free to edit further if not. – Nick Craver May 14 '10 at 11:47
  • @Nick: I've made a couple more edits - check that you agree with them :) – Jon Skeet May 14 '10 at 11:51
  • @Jon - Absolutely, that's 100% clear, what I think of and being able to articulate it clearly don't always go hand in hand, thanks for the edit. – Nick Craver May 14 '10 at 11:52
  • 1
    @maxp - I don't know of a way to do what you're after, you can update `bar.InnerHtml` whenever you want via that property, internally it's not even a string, IIRC it gets transformed into a `LiteralControl` when you set the `InnerHtml`, along with the viewstate setting, you can see this by doing `((LiteralControl)bar.Controls[0]).Text = "zzzzzzz"; //now bar.InnerHtml == "zzzzzzz"` – Nick Craver May 14 '10 at 11:57