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(); } } }
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!
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
NinjaTrader
Inner class accessing outer classes' variables
Collapse
X
-
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'
Code:Tags: None
-
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.
-
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
-
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 johnMoss, 03-04-2024, 01:53 PM
|
3 responses
46 views
0 likes
|
Last Post
by AndyM
Today, 06:13 PM
|
||
Started by algospoke, Today, 06:01 PM
|
0 responses
8 views
0 likes
|
Last Post
by algospoke
Today, 06:01 PM
|
||
Started by cre8able, Today, 05:53 PM
|
0 responses
8 views
0 likes
|
Last Post
by cre8able
Today, 05:53 PM
|
||
Started by Wessel, 03-23-2024, 02:37 PM
|
3 responses
24 views
0 likes
|
Last Post Today, 04:58 PM | ||
Started by martini, Yesterday, 04:45 PM
|
4 responses
32 views
0 likes
|
Last Post
by martini
Today, 04:48 PM
|
Comment