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 cmoran13, 04-16-2026, 01:02 PM
    0 responses
    36 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    23 views
    0 likes
    Last Post PaulMohn  
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    162 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    96 views
    1 like
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    152 views
    2 likes
    Last Post CaptainJack  
    Working...
    X