Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Major mixup in two ASP.NET transactions
Message
From
04/09/2006 14:34:50
 
 
To
04/09/2006 13:37:06
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
VB 8.0
OS:
Windows XP SP2
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01150471
Message ID:
01150765
Views:
45
>>Definitely help. Simple example:
>>public class Framework
>>{
>>    public static int SomeCalculatedValue;
>>
>>    static Framework()
>>    {
>>        // Lot of complicated code could be here to calculate
>>        // the value of SomeCalulatedValue but, for simplicity:
>>        SomeCalculatedValue = 5;
>>    }
>>}
>>The constructor code will only fire when the first instance is instantiated. All subequent instantiations (as you would be doing in, for example, Page_Init) would not run the constructor code and would share the SomeCalculatedValue. E.g:
>>Framework f1 = new Framework(); // f1.SomeCalculatedValue set to 5
>>Framework f2 = new Framework(); // Constructor code not run but SomeCalculatedValue = 5;
>>f1.SomeCalculatedValue = 6;  // Now *all* Framework objects .SomeCalculatedValue will be 6 as well
>>
>
>This sounds quite amazing. Basically, if I understand it correctly, all I would have to do would be to add the static declaration to all global properties and add the static declaration as well to my Initialize() method. Is that correct? So, even if there would be a call to the Initialize() method, on all pages, as it would be declared static, it wouldn't be executed.

Hi,

Not quite. The sample I gave used the constructor (New() in VB if IRRC). Ordinary methods aren't quite the same. Defining ordinary methods as static means they can be called without instantiating the actual object: So given:
public class Framework
{
      public static string SayHello()
      {
        return "Hello";
      }
}
This method could be used like:
string s = Framework.SayHello();
// Rather than:
f = new Framework();
string s = f.SayHello();
// In fact this wouldn't be legal....
Lots of native examples of this behaviour in the .NET framework; Guid.NewGuid() is a classic example....
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform