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 CaptainJack, 05-29-2026, 05:09 AM
          0 responses
          51 views
          0 likes
          Last Post CaptainJack  
          Started by CaptainJack, 05-29-2026, 12:02 AM
          0 responses
          32 views
          0 likes
          Last Post CaptainJack  
          Started by charlesugo_1, 05-26-2026, 05:03 PM
          0 responses
          99 views
          0 likes
          Last Post charlesugo_1  
          Started by DannyP96, 05-18-2026, 02:38 PM
          1 response
          177 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by CarlTrading, 05-11-2026, 05:56 AM
          0 responses
          170 views
          0 likes
          Last Post CarlTrading  
          Working...
          X