Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Reset to default values private variables with public access modifiers

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Reset to default values private variables with public access modifiers

    Hello,

    Is it possible to reset to default values private variables with public access modifier set in #region properties, whose values have been changed throughout the code?

    I mean if for instance a private int with a public access modifier with original value of 2 set in State.SetDefaults. If later in OnBarUpdate this value is changed to 3. Is there a way to make it reset to default (2), or another variable needs to be created to store the original value to then restore it?

    Also, if the above variable has been changed throughout the code OnBarUpdate to 3 and it calculates OnBarClose, when a new bar elapses, will the value of this private variable with public access be again 2 (the default value) or it will remain 3 (so it's not affected by its default value set on State.SetDefaults).

    Thanks in advance!

    #2
    Hello roblogic,

    Thank you for the question.

    The way the question is worded is a little confusion so please correct me if I have misunderstood here.


    Is it possible to reset to default values private variables with public access modifier set in #region properties, whose values have been changed throughout the code?
    From what I can tell you are setting the value of a variable in OnBarUpdate, but want it to go back to the default you set in State.SetDefaults, is this correct?

    I mean if for instance a private int with a public access modifier with original value of 2 set in State.SetDefaults. If later in OnBarUpdate this value is changed to 3. Is there a way to make it reset to default (2), or another variable needs to be created to store the original value to then restore it?
    The public/private modifier really wouldn't matter here as you are resetting the value of the variable in runtime. That is what the new value will be just like when it was set in State.SetDefaults in runtime. Another variable would likely be the easiest solution if you need to retain the original value. For example the SMA indicator has a Period input which does not get reset in code, it is just used. If you have logic which uses a input it should also be the same as the SMA and only use the input but not directly change its value.


    Also, if the above variable has been changed throughout the code OnBarUpdate to 3 and it calculates OnBarClose, when a new bar elapses, will the value of this private variable with public access be again 2 (the default value) or it will remain 3 (so it's not affected by its default value set on State.SetDefaults).
    This is a good use case for Prints to further your understanding of how your script is called, I will detail a test you can do below. The quick answer would be, if you are changing the variabe in OnBarUpdate that value would be retained as you mentioned this is a public/private variable being used which exists at the class level. This would persist across multiple OnBarUpdate calls and because SetDefaults is called before OnBarUpdate the value would never reset back to 2.

    In your script, you can add a print to both OnStateChange and OnBarUpdate like the following to better understand the order your script is executed in:

    Code:
    protected override void OnStateChange()
    {
        Print(State);
    }
    
    protected override void OnBarUpdate()
    {
        Print(State + " " + CurrentBar);
    }

    I look forward to being of further assistance.

    Comment


      #3
      Thanks Jesse for the comprehensive information!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      558 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      324 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      101 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      546 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      547 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X