简体   繁体   中英

c# - maintaining state of a variable everytime an object is created

  static int next;
    partial void OnCreated()
    {
        next = next + 1;
        PurchaseOrderLineNo = next;
        this.RequestDueDate = DateTime.Now;
    }

Issue here is static variable shares its value among all the instances of class.

now if i have added 12 values then thought of deleting them all, now what happens is the next time i add value it should be 1 instead it starts with 13. so im confused how to achieve this scenario.

basically i am trying to show sequence number in my gridcontrol. im using partial class of L2S

If you want to maintain the value of a variable across multiple instances of a class, you need to make that variable static :

private static int PurchaseOrderLineNo { get; set; }

Then you use it without the this keyword, since that is used to reference instance variables:

NextState = PurchaseOrderLineNo + CurrentState;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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