tags:

views:

94

answers:

1

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'
+4  A: 

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.

Nick Craver
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
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
@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
@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
@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
@Jon - Updated, hopefully that's a bit clearer, feel free to edit further if not.
Nick Craver
@jon - thanks for that.
maxp
@Nick: I've made a couple more edits - check that you agree with them :)
Jon Skeet
@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
@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