Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cannot extend DrawObjects.Line

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

    Cannot extend DrawObjects.Line

    Hello,

    I'm trying to create a new class to extend the DrawObjects.Line class using inheritance. Here's the code for the extended class:

    Code:
    		public enum TrendlineType {
    			PrimaryTrend,
    			PrimaryChannel,
    			SecondaryTrend,
    			SecondaryChannel,
    			CounterTrend,
    			CounterChannel
    		}
    			
    	    public class Trendline : NinjaTrader.NinjaScript.DrawingTools.Line
    	    {
    	        public Trendline() : base()
    	        {
    					
    	        }
    			public TrendlineType Type() 
    			{
    				return TrendlineType.PrimaryTrend;
    			}
    				
    	    }
    However, when I try and cast an object of type DrawingObjects.Line, I get the error:

    Unable to cast object of type 'NinjaTrader.NinjaScript.DrawingTools.Line' to type 'Trendline'.

    Code:
    				NinjaTrader.NinjaScript.DrawingTools.Line objLine = objTool as DrawingTools.Line;
    																
    				Trendline objTrendline = (Trendline)new DrawingTools.Line(); // This raises a casting error
    Please let me know if you have any ideas. A full indicator that illustrates the problem is attached. Place the indicator on a chart that has at least one manually drawn trendline, and the error can be seen in the NinjaScript Output window.

    Thank you,

    Ryan
    Attached Files

    #2
    Hello gripload, and thank you for your questions.

    Generally the best way to resolve this scenario, where you would like to downcast a NinjaTrader Line into a TrendLine, is with a static factory method in your TrendLine class with the signature

    Code:
    [FONT=Courier New]public static Trendline newFromLine(NinjaTrader.NinjaScript.DrawingTools.Line line)
    [/FONT]
    You could also simply take in a line as a constructor to a Trendline; there are many ways to implement this. One way or the other, though, you will have to tell C# exactly how to turn a Line into a more specific kind of Line.

    I am including a publicly available link to a Stack Overflow discussion on this topic. Please let us know if there are any other ways we can help.

    I am new to C# (and OOP). When I have some code like the following: class Employee { // some code } class Manager : Employee { //some code } Question 1: If I have other code that does this...
    Jessica P.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    579 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    334 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
    554 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    551 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X