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

Inner class accessing outer classes' variables

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

    Inner class accessing outer classes' variables

    I want an inner class to access the variables of the outer class (strategy). Is this possible (without passing the outer class as reference to the inner class). I get the following error when trying to compile:

    Cannot access a non-static member of outer type 'NinjaTrader.Strategy.testInnerClass' via nested type 'NinjaTrader.Strategy.testInnerClass.InnerClass'
    on the bolded code below:

    Code:
    namespace NinjaTrader.Strategy
    {
        public class testInnerClass : Strategy
        {
    
       		public double close_px;
    		private InnerClass inner = new InnerClass();
    		
    
    		private class InnerClass{
    		
    			private double myClose;
    			
    			public void Update(){
    				[B]myClose = close_px;	[/B]
    			}
    		}
         
            protected override void Initialize()
            {
                CalculateOnBarClose = false;
            }
    
    
            protected override void OnBarUpdate()
            {
    			close_px = Close[0];
    			inner.Update();
            }
        }
    }

    #2
    The error tells you what you need to do. Make it a static class. Alternatively, as you seem to be only passing a close, you could use a transparent struct. It would be a lot cleaner and you wouldn't run into instantiation issues. Hope this helps.

    Try this:

    public static class testInnerClass
    {
    public double close_px;

    private static class InnerClass
    {
    private static double myClose;
    public static void Update()
    {
    myClose = close_px;
    }
    }
    }

    and then reference it as
    InnerClass.Update();

    Don't need to instantiate.
    Last edited by Zeos6; 01-19-2013, 04:41 PM.

    Comment


      #3
      Thanks, that compiles, though I may need non-static classes in the future.

      How how were you able to format the code to look like that?

      Comment


        #4
        If you mean the code below, I simply pasted the code from testing it in an actual NT script.

        I understand that you may want non-static classes in the future but I would urge you to think it through. Instantiating a lot of classes will degrade performance - both memory as well as GC, and you are relying on the JIT to optimize your code. In the case you show below, where you only are interested in the close, I would go with a transparent struct rather than a static class. It would be cleaner code and would provide substantially better performance.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by fitspressoburnfat, Today, 04:25 AM
        0 responses
        2 views
        0 likes
        Last Post fitspressoburnfat  
        Started by Skifree, Today, 03:41 AM
        1 response
        4 views
        0 likes
        Last Post Skifree
        by Skifree
         
        Started by usazencort, Today, 01:16 AM
        0 responses
        1 view
        0 likes
        Last Post usazencort  
        Started by kaywai, 09-01-2023, 08:44 PM
        5 responses
        604 views
        0 likes
        Last Post NinjaTrader_Jason  
        Started by xiinteractive, 04-09-2024, 08:08 AM
        6 responses
        23 views
        0 likes
        Last Post xiinteractive  
        Working...
        X