Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using TimeSpan as a Parameter

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

    Using TimeSpan as a Parameter

    Hi,
    I'm have a input parameter that needs to be a time. I set it's type as a timespan object. In order to have it save it's values I added the following:

    Code:
    [NinjaScriptProperty]
    [Display(Name="OpenTime", Description="Open Time", Order=1, GroupName="Parameters")]
    public TimeSpan OpenTime
    { get; set; }
    
    [Browsable(false)]
    public string OpenTimeSerialize
    { 
    	get { return OpenTime.ToString(); } 
    	set { OpenTime = TimeSpan.Parse(value); } 
    }
    Is this the correct approach? I just kind of emulated what was done for brushes. I assume this is the approach for most parameters that are not base types?

    If this is correct you might want to consider adding a blurb into the documentation about this.

    #2
    Hello,

    That should do the trick for you. Are you running into any issues with that method? The serialization is the crucial component of exposing TimeSpans, and it looks like your method should cover that requirement.
    Dave I.NinjaTrader Product Management

    Comment


      #3
      Hi Dave,
      Seems to be working fine. I was just wondering if there are any general rules for when parameters need the extra serialization method? Also, what are the naming rules for the serialization method? Does it just always need to be PropertyName + Serialize ?
      Last edited by GrumpyTrader; 09-14-2015, 10:28 AM.

      Comment


        #4
        Adding "Serialize" to the end of the property name is considered a best practice, but is not strictly required. It can be helpful to immediately differentiate the serializable property from any others you may have with similar names.

        As I was searching the web for a concise answer to the question of "when should I serialize something?," the answer that I came across time and time again was "only when you need to!" Simply put, you will want to serialize something specifically when it cannot be set, re-loaded, shared between classes/apps, or stored in its native form. A timespan is a perfect example of this. Since there is not really a way for a user to define a TimeSpan per se in a UI input field, you need a way to translate the TimeSpan's parameters into something that can be obtained from user input. Serialization saves the day here. So the short answer could be "when you need to do something with an object that cannot be directly done with that object, but it can be done with string representations of the object's properties, then serialize."
        Dave I.NinjaTrader Product Management

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by algospoke, Yesterday, 06:40 PM
        2 responses
        19 views
        0 likes
        Last Post algospoke  
        Started by ghoul, Today, 06:02 PM
        3 responses
        14 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by jeronymite, 04-12-2024, 04:26 PM
        3 responses
        45 views
        0 likes
        Last Post jeronymite  
        Started by Barry Milan, Yesterday, 10:35 PM
        7 responses
        21 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by AttiM, 02-14-2024, 05:20 PM
        10 responses
        181 views
        0 likes
        Last Post jeronymite  
        Working...
        X