PDA

View Full Version : Variable reference problem


rhbishop5
01-17-2010, 09:22 AM
Hi: I'm creating an Indicator. I declare a variable JU. In the Initialize section I assign JU a calculated value intended to be used as a constant later in the program. I can now print that value and it is correct.

Now I use that variable in the OnBarUpdate part of my indicator. JU shows equal to 0 rather that the assigned calculated value from the initialize section. How do I pass the value of JU from the initialize section so I can use it in the ONBarUpdate section??

Any help or suggestions truly appreciated.

Ralph
01-17-2010, 10:22 AM
It sounds like you declared and assigned that variable in Initialize(). If you declare in the variables section of your class, then it will work as expected (modifications done in Initialize() are visible in OnBarUpdate()).

Regards
Ralph

Welcome to the forum, rhbishop5.

rhbishop5
01-17-2010, 11:28 AM
Thanks, Ralph. The variable was declared in the Variables section by private double JU; It was then assigned a value in the initialize section by double JU=(-100/(U-100))-1; . When printed in this section, JU shows the proper value. When used in the OnBar Update area it comes up as 0.

Ralph
01-17-2010, 11:55 AM
Attached you find an example that works as I would expect to. An integer is defined in the variables section then modified in Initialize() and finally displayed on a chart as depicted in the little picture. Now you could use this code example to demonstrate where your problem is.

Regards
Ralph

rhbishop5
01-17-2010, 12:56 PM
YES!!!!! Thanks, Ralph. That did the trick. I had a "double" in front of the variable when I assigned it in the initialize area and evidently the compiler considers that a separate variable not related too the one declared in the variable area. Thanks much!!!! Bob

Ralph
01-17-2010, 01:08 PM
Glad to see, that variable assignments work too for your Ninja installation, Bob :)

The lesson in your case is to take care when and where to declare a variable. C# is very flexible, you could declare a variable nearly everywhere, Although a declaration at the beginning of a method is a viable spot. More strict compilers with the possibility to write out log messages (not only compiler errors) sometimes issue warnings about name-clashes.

Regards
Ralph

NinjaTrader_Austin
01-17-2010, 09:40 PM
Bob, great to hear you've found a working solution. As you've found out, this is a pretty helpful forum. If you have any other questions, just ask.

Ralph, thank you for your continued support and contributions to the forum.