How to access a list outside a class.

FranK Duc 121 Reputation points
2021-09-02T17:59:37.097+00:00

Hello,

I am trying to acces a list outside a class. I tried with public static but nothing. The list return different answers than if it called from OnRender class. Any suggestions!

 public class NEWSDBA : Indicator
    {

public static var listm = new List<double>();    //tried with private also

protected override void OnBarUpdate()
        {
foreach(var item in listm)
{
Value[0] = item;
}

        }


protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
        {
            base.OnRender(chartControl, chartScale); 

//calculation to produce the list start here

       }


}

Thank you

Developer technologies | C#
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2021-09-02T18:13:13.957+00:00

    Static class members are not part of the class instance by definition. Typically, a public property is used.

        public class MyClass
        {
            public List<double> myDouble { get; set; }
    
            public MyClass()
            {
                myDouble = new List<double>() { 1, 2, 3, 4, 5 };
            }
        }
        class Program
        {
    
            static void Main(string[] args)
            {
                MyClass myclass = new MyClass();
                foreach(double item in myclass.myDouble)
                {
                    Console.WriteLine(item);
                }
            }
        }
    
    0 comments No comments

  2. FranK Duc 121 Reputation points
    2021-09-02T18:24:37.63+00:00

    I get an error message saying :

    NinjaScript File Error Code Line Column
    NEWSDBA.cs 'NinjaTrader.NinjaScript.Indicators.Indicator.NEWSDBA (int, int)' is a 'method', which is not valid in the given context CS0119 78 34

    I think you got it right but i dont know how to make it work, considering it is Ninjatrader software and not Visual studio. Any inputs?

     public class NEWSDBA : Indicator
        {
    public List<double> listm { get; set; }
    
    
    
    protected override void OnBarUpdate()
            {
    NEWSDBA newsdba = new NEWSDBA();
    
     foreach(var item in NEWSDBA.listm)
                 {
     Value[0] = item;
      }
            }
    
    
    }
    

    Thanks


  3. P a u l 10,761 Reputation points
    2021-09-02T18:41:31.25+00:00

    You're accessing listm like it's a static property. Did you mean to use newsdba here instead of NEWSDBA?:

    foreach(var item in NEWSDBA.listm)
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.