Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 th...
    Jessica P.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by JoMoon2024, Today, 06:56 AM
    0 responses
    6 views
    0 likes
    Last Post JoMoon2024  
    Started by Haiasi, 04-25-2024, 06:53 PM
    2 responses
    17 views
    0 likes
    Last Post Massinisa  
    Started by Creamers, Today, 05:32 AM
    0 responses
    5 views
    0 likes
    Last Post Creamers  
    Started by Segwin, 05-07-2018, 02:15 PM
    12 responses
    1,786 views
    0 likes
    Last Post Leafcutter  
    Started by poplagelu, Today, 05:00 AM
    0 responses
    3 views
    0 likes
    Last Post poplagelu  
    Working...
    X