2

I have a class to display colors in order

public class Color
{
     private static string[] _colors = {"Red", "Green", "Blue"};
     public static int Index { get; set; }

     public static string GetNextColor()
     {
         var retVal = _colors[Index];

         Index++;

         return retVal;
     }
}

I am using the above class by this following code:

 [HttpGet]
 public string TestColor()
 {
     Color.Index = 0;
     return string.Format("First color: {0} Second color: {1} Third color: {2}", Color.GetNextColor(), Color.GetNextColor(), Color.GetNextColor());
 }

which display this result

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
First color: Red Second color: Green Third color: Blue
</string>

Now my question is

How can I reset the Index into 0 every time a page reload without resetting it as Color.Index = 0;

Based on my knowledge a static field can only be reset by app restart, so in my application, I'm always being forced to reset the Index to 0 everytime I call this static method. I just want this field "Index" to reset to 0 every time the page reload. I'm not sure if setting Color.Index = 0 for every top of my code can be avoided.

tRuEsAtM
  • 3,517
  • 6
  • 43
  • 83

1 Answers1

0

If you want a variable that is reset with every page request, you should make it a member variable and not a static variable. Your views, controllers, models, and pipeline objects are all torn down and recreated fresh for each HTTP request, so it'll reset the color automatically.

If you insist on doing it with a static variable, you could just add a line of code to Application_BeginRequest that sets it to 0.

John Wu
  • 50,556
  • 8
  • 44
  • 80
  • I'm interested at making it as member variable, but can I access the member variable in a static method? Do you have sample, thanks –  Feb 23 '17 at 00:26
  • Why does it have to be a static method? – John Wu Feb 23 '17 at 00:30
  • Because I need to use it inside different methods. –  Feb 23 '17 at 00:32
  • First part of your answer is not correct. You can't be perfectly sure if HTTP request creates fresh instance of this class and reset non static variable. It depends on API (or DI) configuration, and request lifetime scope is only one of the many options. – Michał Jarzyna Feb 23 '17 at 00:34
  • [I respectfully disagree](http://stackoverflow.com/questions/5425920/asp-net-mvc-controller-created-for-every-request) – John Wu Feb 23 '17 at 00:36
  • But what about custom controller factory with [ninject](http://www.philhack.com/configure-asp-net-mvc-5-with-ninject/) for example? You can put [custom scope](https://github.com/ninject/ninject/wiki/Object-Scopes) for every binding. – Michał Jarzyna Feb 23 '17 at 00:59
  • If you really have built a custom controller factory, and you had the nerve to re-use controller object instances, you are probably much more advanced than the OP. For someone like Jorge who is trying to figure out how static variables work in ASP.NET, I'm thinking my answer is probably at the right level of detail. – John Wu Feb 23 '17 at 01:08
  • Yes you are right and this is the reason why I only put the comment below your answer. For OP it is propably perfect solution, but other readers should know this is only shortcut for basic usage. – Michał Jarzyna Feb 23 '17 at 01:20