.NET / C# String Reference in Code Behind Not Loading Updated Value
Asked Answered
C

2

4

I seem to have run into a strange issue whereby a string reference is not loading an updated value.

In short, I've replaced a DLL file (App_Code.dll) that contains a bunch of page titles (think HTML Page Titles), but the values aren't being updated when referenced from other DLL's.

Here's a code snippet from a codebehind of a sample page:

   this.Master.PageDescription = Constants.Titles.CardInfoPageDescription;

The Constants class is compiled into App_Code.dll, which I just replaced. I've also cleared the cache (IIS 6 in this case), rebooted the machine, and made sure my local browser cache is empty.

However, when I load the web page, it is not loading the new value for Constants.Titles.CardInfoPageDescription. This is true for ALL web pages.

The only way I can get it to update it to replace the DLL for that page, which hasn't changed at all...

Any idea why this is? Is this string reference not actually looked up at runtime and built into the page DLL's?

Any help is GREATLY appreciated!

Thanks, Adam

Chinch answered 19/8, 2010 at 15:13 Comment(0)
S
5

Let me guess: your constants are exposed as public const fields.

Whenever you use a const, its value is embedded into the compiled code at build-time rather than being referenced dynamically at run-time. So when you subsequently replace the DLL where the constants are declared, all code outside of the replaced DLL will continue to use the old value until it is recompiled.

On a more philosophical note - why are your "constants" being updated? Only use const for values that will never, ever, ever change. If it can change then it's not a constant.

And on a more practical note - it's not generally considered good practice to expose public fields. Use properties instead. (One possible exception to this rule might be genuine constants that will never, ever, ever change.)

Slighting answered 19/8, 2010 at 15:15 Comment(4)
Constants are compiled into each dll that uses it rather than referenced.Luannaluanne
As an FYI - these were page titles, that we thought would never change, but we decided to to a 1 time refresh of the layout for SEO purposes. Definitely something we've gone away from using constants now that we know this... Thanks so much!Chinch
it's also not inconceivable that the value is meant to be a constant, but during development a BA may change the requirement for the value so it would change at this time unexpectedly. This is very handy to know for the future though as I also thought that referenced constants remained external.Ikon
@Joel: That's why I said that you should only use const for values that will never change: CentimetresPerMetre, AbsoluteZeroInCelsius, PiTruncatedToFiveDecimalPlaces etc are genuine constants; SomeValueThatTheSpecPromisedWouldNeverChange isn't.Slighting
S
1

Constants are converted at Compile time to their respective values and thus not changed at Runtime. References of these constants will be built at compile time in these referencing DLL's.

Saxtuba answered 19/8, 2010 at 15:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.