Static Values
-
Hello all I was under the impression that if I make a call into a static function which uses the value of a static class variable, if I recall the function later the value of the static class variable would reset back to the default coded value. Now as the following example prooves I am obviously wrong. I have a test application/dll that has the following code : StaticLib.dll
using System;
namespace StaticLib
{
public class MyStaticLib
{
private static int val = 0;
public static int GetNumber()
{
return val++;
}
}
}Console Application (which references StaticLib.dll)
using System;
using StaticLib;
using System.Threading;namespace StaticTest
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
while(true)
{
Console.WriteLine(MyStaticLib.GetNumber());
Thread.Sleep(1000);
}
}
}
}When I ran this for the first time I expected to get the following output
0
0
0
0
0
0But I actually get
1
2
3
4
5
6
7Could anybody explain why this is? post.mode = postmodes.signature; SELECT everything FROM everywhere WHERE something = something_else; > 1 Row Returned > 42
-
Hello all I was under the impression that if I make a call into a static function which uses the value of a static class variable, if I recall the function later the value of the static class variable would reset back to the default coded value. Now as the following example prooves I am obviously wrong. I have a test application/dll that has the following code : StaticLib.dll
using System;
namespace StaticLib
{
public class MyStaticLib
{
private static int val = 0;
public static int GetNumber()
{
return val++;
}
}
}Console Application (which references StaticLib.dll)
using System;
using StaticLib;
using System.Threading;namespace StaticTest
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
while(true)
{
Console.WriteLine(MyStaticLib.GetNumber());
Thread.Sleep(1000);
}
}
}
}When I ran this for the first time I expected to get the following output
0
0
0
0
0
0But I actually get
1
2
3
4
5
6
7Could anybody explain why this is? post.mode = postmodes.signature; SELECT everything FROM everywhere WHERE something = something_else; > 1 Row Returned > 42
MrEyes wrote: I recall the function later the value of the static class variable would reset back to the default coded value. That is not correct. The value will not reset unless you have code to explicitly reset it. MrEyes wrote: Could anybody explain why this is? That is the correct behaviour. A static is a value that exists for all instances of the class. If some code modifies it then all instances of the class (and any static methods/properties) see the modification. Some people think of a static that is a global value within the class. Does this help?
My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
-
Hello all I was under the impression that if I make a call into a static function which uses the value of a static class variable, if I recall the function later the value of the static class variable would reset back to the default coded value. Now as the following example prooves I am obviously wrong. I have a test application/dll that has the following code : StaticLib.dll
using System;
namespace StaticLib
{
public class MyStaticLib
{
private static int val = 0;
public static int GetNumber()
{
return val++;
}
}
}Console Application (which references StaticLib.dll)
using System;
using StaticLib;
using System.Threading;namespace StaticTest
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
while(true)
{
Console.WriteLine(MyStaticLib.GetNumber());
Thread.Sleep(1000);
}
}
}
}When I ran this for the first time I expected to get the following output
0
0
0
0
0
0But I actually get
1
2
3
4
5
6
7Could anybody explain why this is? post.mode = postmodes.signature; SELECT everything FROM everywhere WHERE something = something_else; > 1 Row Returned > 42
This behavious is correct. Static variable has lifetime of global variable but it has a scope of local variable. Hence the value of static variable persists across multiple calls to the function. Unlike nonstatic members where each instance of object has its own copy of variables, the static variable is shared across all instances i.e. only one copy od static variable is available across all instances of objects. Ravindra Sadaphule MCSD.NET