public static string s = "NOT initialized"; //my default value
s = "Initialized";
VS 2012, What I am seeing is;...when I expect it to equal "NOT initialized".Why do you expect this?
public static string s = "NOT initialized"; //my default value
to be run and initialize again, to Not Initialized, later it gets Initialized to the proper value dependent on where the code is being run.public static string s = "Set to default"; //my default value
and
s = "Set to run";
. That would have made a much clearer question. I would have expected it to be "Set to Default" instead of persisting at "Set to run"to be run and initialize again, to Not Initialized, later it gets Initialized to the proper value dependent on where the code is being run.That is exactly what happens. A static variable is only going to retain its value while the program is running.. Once your program terminates, it is unloaded from memory by the system--all of it: static, non-static, and otherwise. The next time you run the application, it's as if you've never run it. Programs don't just sit in memory. Your initialization code runs every time you start the application. Whether or not the variable's value gets changed to something else is dependent on how your code is written.
I read that the static variables persists while the app domain is live?Correct.
maybe IIS is the app domainIIS creates an app domain and loads your app into it, yes.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Scratch_Debug.aspx.cs" Inherits="WebApplication1.Debug.Scratch_Debug" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblStaticTest" runat="server" Text="lblStaticTest"></asp:Label><br />
<asp:Button ID="btnSetS" runat="server" Text="btnSetS" OnClick="btnSetS_Click" />
</div>
</form>
</body>
</html>
namespace WebApplication1.Debug
{
public partial class Scratch_Debug : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
StaticTest StaticTestInitialize = new StaticTest(); //execute static constructor
}
lblStaticTest.Text = StaticTest.s;
}
protected void btnSetS_Click(object sender, EventArgs e)
{
StaticTest.s = "Not Default";
lblStaticTest.Text = StaticTest.s;
}
}
public class StaticTest
{
public static string s
{
get
{
return _s;
}
set
{
_s = value;
}
}
private static string _s;
static StaticTest()
{
_s = "Default";
}
}
Thanks much. I tried to use the constructor (failed) but want to try again with more knowledge...
The reason I failed was I could not figure out where to instantiate an object of the class before anything else ran, in other words what which page runs first. I have read on this "singleton" pattern, I think I can attempt to instantiate it in all the required places and only the first one will execute, setting my default properties. On each run. My fear is the static class will still be instantiated on re-run. I am trying to test this now.
Please if you have better solution or comment on this approach or...
Kind Regards
Sam
Open in new window