Object Reference is Required Compile Error (CS0120 )

‘’’

//This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
public class MyCustomTestIndicator : Indicator
{
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @“Enter the description for your new custom Indicator here.”;
Name = “MyCustomTestIndicator”;
Calculate = Calculate.OnBarClose;
IsOverlay = false;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
//Disable this property if your indicator requires custom values that cumulate with each new market data event.
//See Help Guide for additional information.
IsSuspendedWhileInactive = true;
}
else if (State == State.Configure)
{
}
}

	private class testClass()
	{
		int a = CurrentBar;		
		
	}

	protected override void OnBarUpdate()
	{
		//Add your custom indicator logic here.
	}
}

}

I get a compile error,
MyCustomTestIndicator.cs,An object reference is required for the non-static field method or property ‘NinjaScriptBase.CurrentBar’,CS0120,55,12,

Why is that?
First time in the new forum, is this the correct category?
thankx

Hi and welcome, The problem you have there is that you are accessing CurrentBar from a sub-class. CurrentBar is a property of the indicator itself. You’ll need to alter your subclass to take a script property or just use member functions instead.

public class testClass()
	{
           NinjaScriptBase _caller;
               
           public testClass(NinjaScriptBase caller)
           {
                 _Caller = caller;
           }

           public void DoStuff()
           {
	           int a = _caller.CurrentBar;		
           }
		
	}
1 Like