Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Setting properties in a class

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

    Setting properties in a class

    I am using a helper class and I set a lot of properties from that class.

    Example :

    Code:
    public class Helper
    {
          public Brush thisBrush = Brushes.Red;
          public SharpDX.Direct2D1.Brush thisBrushDx  = null;
    }
    
    
    In my indicator properties section :
    
    [XmlIgnore]
    public Brush thisBrush
    {
           get { return helper.thisBrush; }
    	set 
    	{ 
    		helper.thisBrush = value;
    				
    		if( helper.thisBrush == null )
    		   return;
    			
    		if( helper.thisBrush.IsFrozen)
    		    helper.thisBrush= helper.thisBrush.Clone();
    				
    		helper.thisBrush.Freeze();
    				
    	  }
    }
    		
    [Browsable(false)]
    public string ThisBrushSerialize
    {
    	get { return Serialize.BrushToString(thisBrush); }
    	set { thisBrush = Serialize.StringToBrush(value); }
    }
    
    public override void OnRenderTargetChanged()
    {
          if(helper.thisBrushDx != null)
            helper.thisBrushDx.Dispose();
    
          if(RenderTarget != null)
          {
                helper.thisBrushDx = helper.thisBrush.ToDxBrush(RenderTarget);
          }
    }
    
    
    And on StateChange
    if(State == State.Terminated)
    {
        if(helper.thisBrushDx != null)
        {
             helper.thisBrushDx.Dispose();
             helper.thisBrushDx = null;
        }
    }

    I am not using State.SetDefaults from OnStateChange() at all to set Brush and SharpDx.Direct2D1.Brush.


    Is that the right pattern to use ?
    Am I wrong in some way ?

    #2
    Hello blar58,

    Thanks for opening the thread.

    As far as I can tell you are serializing the brushes correctly, freezing when you are supposed to and are recreating DxBrushes within the context of OnRenderTargetChanged() as is directed in our help guide.

    As the code is, I get an error when adding the indicator to a chart when I include the helper class. You may wish to use a static class instead to hold a certain data for your Brushes within a helper class.

    Our colleague Jesse has a good sample script that demonstrates using static classes to hold some information external to a NinjaScript. http://ninjatrader.com/support/forum...24&postcount=3

    Best practices for using Brushes and using SharpDx for custom rendering can be referenced below.

    Working with Brushes - https://ninjatrader.com/support/help...th_brushes.htm

    Using SharpDx for Custom Rendering - https://ninjatrader.com/support/help..._rendering.htm

    Please let me know if I may be of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    81 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    42 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    64 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    66 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    54 views
    0 likes
    Last Post CarlTrading  
    Working...
    X