Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

property range validation

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

    property range validation

    Is there a way to Validate a range of number that can be entered as property input.

    Is there an attribute I can use ? Something like [Range(0-4)] ?


    Thank you

    #2
    Hi blar58,

    Yes, you can use Math.Max() and Math.Min() to restrict the range of inputs in a property. See this post for help with inputs and an example minimum:


    To set the upper-bound as well, nest multiple statements together, like:
    [Description("Numbers of bars used for calculations")]
    [GridCategory("Parameters")]
    public int Period
    {
    get { return period; }
    set { period = Math.Min(Math.Max(1, value), 4); }
    }
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Ryan

      Yes I knew about this but let's say I enter 105 and the acceptable range value is 0-4 so I put Math.Min(4,value) 4 will be the result. would like it to be 0 if it is greater than 4.

      Is there a way around it

      Thanks

      Comment


        #4
        OK Ryan I figured that out.

        Thanks

        Comment


          #5
          You could build your own logic there to handle inputs exactly as you like.

          If the value inputted is over 4, make it 0. Otherwise the value must be at least 0:

          Code:
          [Description("")]
          [GridCategory("Parameters")]
          public int MyInput0
          {
               get { return myInput0; }
               set { myInput0 = value > 4 ? 0 : Math.Max(value, 0); }
          }
          Ryan M.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by CarlTrading, 03-31-2026, 09:41 PM
          1 response
          156 views
          1 like
          Last Post NinjaTrader_ChelseaB  
          Started by CarlTrading, 04-01-2026, 02:41 AM
          0 responses
          90 views
          1 like
          Last Post CarlTrading  
          Started by CaptainJack, 03-31-2026, 11:44 PM
          0 responses
          138 views
          2 likes
          Last Post CaptainJack  
          Started by CarlTrading, 03-30-2026, 11:51 AM
          0 responses
          130 views
          1 like
          Last Post CarlTrading  
          Started by CarlTrading, 03-30-2026, 11:48 AM
          0 responses
          107 views
          0 likes
          Last Post CarlTrading  
          Working...
          X