简体   繁体   English

静态与公共财产

[英]Static vs public property

i was writing code in MVC and i don't' understand behaviours public properties in my project!! 我在用MVC编写代码,但我不了解我项目中的行为公共属性! 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. 仅当我具有STATIC属性时它才起作用,但是当我具有PUBLIC时,增量不起作用。 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. 我想增加我的属性firstDayWeek中的日期。

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

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. 但是,对于实例变量,该属性也是新的,并在创建控制器的新实例时进行初始化。

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. 但是,如果您使用static修饰符声明任何函数或变量,它将直接在内存中创建实例并全局作用。 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#? 有关更多说明,请参考此内容。C#中静态变量的用途是什么? When to use it? 什么时候使用? Why can't I declare the static variable inside method? 为什么我不能在方法内部声明静态变量?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM