question

FranKDuc-4126 avatar image
0 Votes"
FranKDuc-4126 asked AgaveJoe commented

How to access a list outside a class.

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

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AgaveJoe avatar image
0 Votes"
AgaveJoe answered

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);
             }
         }
     }


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

FranKDuc-4126 avatar image
0 Votes"
FranKDuc-4126 answered AgaveJoe commented

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

· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The code sample makes no logical sense. I'm not sure what you are trying to do.

Typically, any method that starts with "On" is a part of an event driven pattern. I'm guessing that List<double> is an event argument???

0 Votes 0 ·

Cant say i am starting with c#

0 Votes 0 ·

Cant say i am starting with c#

I do not understand your response. Maybe this is what you are trying to do??? The Program class updates the MyClass.myDouble property. The property is updated outside the class.

     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);
             }
    
             for(int i = 0; i < myclass.myDouble.Count(); i++)
             {
                 myclass.myDouble[i] = i * 10;
             }
    
             Console.WriteLine("-----------");
    
             foreach (double item in myclass.myDouble)
             {
                 Console.WriteLine(item);
             }
         }
     }

Results

 1
 2
 3
 4
 5
 -----------
 0
 10
 20
 30
 40


0 Votes 0 ·
Show more comments
Paul-5034 avatar image
0 Votes"
Paul-5034 answered Paul-5034 commented

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)

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You are right, wrong spelling. But it does not solve the issue.

0 Votes 0 ·

Do you need this line?

NEWSDBA newsdba = new NEWSDBA();

I'd assume you'd just want to foreach over the listm for the current instance, i.e.

foreach(var item in listm)

As long as listm is being initialised somewhere (like in the constructor as AgaveJoe posted) then that should work.

This line will also overwrite the first item in the Value array for every item in listm - is that deliberate?

Value[0] = item;

0 Votes 0 ·

Without that line NEWSDBA newsdba = new NEWSDBA();
newsdba dont exist in that context.

0 Votes 0 ·
Show more comments

Without that line newsdba dont exist.

0 Votes 0 ·