MarketSeries and class
MarketSeries and class
15 Nov 2017, 18:52
Hi everyone,
I have created a class ...
Inside that class, the constructor uses data from Parameters and MarketSeries.
The class is called inside my indicator and at this point all works perfectly.
I decided to clean my code, and instead of passing Parameters several times for each methods and sub methods, i thought it may have been better to use Parameters as a global variable inside the calss.
So i created a new private field for the Parameter... so far so good, i can call the parameter everywhere in the class , no need to pass it as argument in every methods.
BUT ...
I tried to do the same thing with MarketSeries (as it's everywhere in my methods) but it doesn't work.
It builds successfully but the indicator draws nothing and in the log i have this error message:
"Crashed in calculate with nullReferenceException: Object reference not set to an instance of an object."
Is it possible to pass MarketSeries as a variable that can be used by methods ? or do i have to pass it as argument in every method (as i did before)?
What is the most 'elegant' way to do it ?
This works:
//... public class MyAmazingClass { //Fields------------------------- public int Var1; public double I_Did_something; private int Parameter1; //Constructor--------------------- public MyAmazingClass(MarketSeries marketSeries, int var1, int parameter1) { Var1 = var1; Parameter1 = parameter1; I_Did_something = Do_Something(var1, marketSeries); } //Methods-------------------------- private double Do_Something(int var1, MarketSeries MarketSeries) { var result = Parameter1 + MarketSeries.Low[var1] return result; } } //...
This doesn't work :
//... public class MyAmazingClass { //Fields------------------------- public int Var1; public double I_Did_something; private int Parameter1; private MarketSeries marketSeries; //Constructor--------------------- public MyAmazingClass(MarketSeries ms, int var1, int parameter1) { Var1 = var1; Parameter1 = parameter1; marketSeries = ms; I_Did_something = Do_Something(var1); } //Methods-------------------------- private double Do_Something(int var1) { var result = Parameter1 + marketSeries.Low[var1] return result; } } //...
...in both cases, the class is called from Calculate:
//... public override void Calculate(int index) { //... amazing_object = new MyAmazingClass(MarketSeries, index, parameter1); //... } //...
I just have few weeks of experience using c# and i may not have used the right words to explain the problem... anyway i hope this question makes sense ...