Announcement

Collapse
No announcement yet.

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 Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        574 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        332 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
        553 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