1

i was writing code in MVC and i don't' understand behaviours public properties in my project!! my code its very simple, increment property. When i clik on button the property is overwrites. Its work only when i have STATIC property, but when i have PUBLIC, increments doesnt work. public propertys are fleeting? Could you help me understand this, its my controller:

  public class DatsController : Controller
{
    // GET: Dats
    Dats dats;
    static public int _Var
    {
        get; set;
    }
    public DatsController()
    {           
            dats = new Dats();      
    }

    public ActionResult Index()
    {
         if (Request.HttpMethod == "post")
         {
             ViewBag.zmienna = _Var;
             dats.dayToday = DateTime.Now.Date;

         }
         else
         {
            // var = var + 5;
             ViewBag.zmienna = _Var;
         }

        return View();
    }

    public ActionResult Next()
    {
        _Var = _Var + 5;
        //dats.firstDayWeek = data.firstDayWeek.AddDays(7);
        return RedirectToAction("Index", "Dats");
    }
}

ok thx for your answers!! I was read,and i was wrote another code and still not working.... I know where is issue but i dont know how to fix them ;/ if you could help me i will be greatfull :)

My model:

 public class Dats
{
     static private DateTime _dataToday;
     static private DateTime _firstDayWeek;
     static private DateTime _lastDayWeek;

     static public DateTime DayToday { get { return _dataToday = DateTime.Now.Date; } }
     static public DateTime FirstDayWeek { get { return _firstDayWeek = Dats.pierwszyDzienTygodnia(_dataToday); } set { _firstDayWeek = value; } }
     static public DateTime LastDayWeek { get { return _lastDayWeek = Dats.ostatniDzienTygodnia(_dataToday); } set { _lastDayWeek = value; } }

    static public DateTime pierwszyDzienTygodnia(DateTime data)
    {

        int dzien = DayOfWeek.Monday - data.DayOfWeek;
        if (dzien > 0)
        {
            dzien -= 7;
        }
        return data.Date.AddDays(dzien);
    }
    static public DateTime ostatniDzienTygodnia(DateTime data)
    {
        DateTime ostatniDzien = pierwszyDzienTygodnia(data.Date);
        return ostatniDzien.Date.AddDays(6);

    }

}

my controller:

public class DatsController : Controller
{

    public ActionResult Index()
    {

         return View();
    }

    public ActionResult Next()
    {
        Dats.FirstDayWeek = Dats.FirstDayWeek.AddDays(7);
        return RedirectToAction("Index", "Dats");
    }
}

my view:

@using (Html.BeginForm("Next", "Dats", FormMethod.Post))
{
    <button type="submit">Next</button>
    @*@Html.ActionLink("dalej","Next", "Visits",new {data =7})*@

    <dl class="dl-horizontal">
        @Dats.DayToday
        <dd>@Dats.FirstDayWeek</dd>
        <dd>@Dats.LastDayWeek</dd>
    </dl>
}

and i would like to increse dates in my property firstDayWeek.

Martin Folkeseth
  • 317
  • 1
  • 11
rejentt
  • 81
  • 6
  • Don't, as in *do not*, use static properties with controllers. Controllers are *not* thread-safe, so a static property will be shared not just between multiple requests of a particular user, but all requests of all users. In other words, you're leaking data between distinct clients. – Chris Pratt Sep 03 '14 at 14:09

3 Answers3

1

Static qualifier for a variable means that the variable is defined once. When you don't have the static qualifier the variable will be defined every time you create an instance of your class. Example:

class A{
  public static int c;
};
class B{
  public int c;
}

A a = new A();
a.c =0;
a.c++;//1
A aa = new A();
aa.c++;//2

B b= new B();
b.c++;//1;
B bb = new B();
bb.c++;//1
Emil Condrea
  • 9,705
  • 7
  • 33
  • 52
1

The question should be Static vs Instance property. The functionality is the basic diffrence between the two. Static property is shared among all the objects of the class. So whenever you make new request a new instance of the controller class is created. Since the property is static it is available for the new instance of the controller to get and set it. However in case of instance variable the property is also new and initialized when the new instance of the controller is created.

Priyank
  • 1,353
  • 9
  • 13
0

Whenever you write a function or declare a variable, it doesn’t create instance in a memory until you create object of class. But if you declare any function or variable with static modifier, it directly create instance in a memory and acts globally. The static modifier doesn’t reference with any object. So they are like single copy in the memory, shared by all. You should use static variables only for hardcoded values for your application. Refer this for more clarification What is the use of static variable in C#? When to use it? Why can't I declare the static variable inside method?

Community
  • 1
  • 1
Shivang MIttal
  • 990
  • 1
  • 14
  • 36