PDA

View Full Version : Exception when compiling a strategy


shliang
10-06-2007, 03:29 AM
Suppose I have the following code snippet:

LinkedList<int> a = new LinkedList<int>();
a.AddLast(1);
foreach(int i in a){
}

A window pops up at compilation because of the foreach statement. But then even the foreach is removed, NinjaTrader cannot compile the strategy. Only a restart can solve this problem.

Here's what I don't understand:

1) foreach works well in .NET on LinkedList, why not in NinjaTrader?

2) why I have to restart NinjaTrader because of a bug or mis-use of keywords in my program? (If Visual Studio .NET does this, I'd short MSFT!)

shliang
10-06-2007, 03:43 AM
Perhaps I was wrong. NinjaTrader's exception report might not be relevant to foreach. Since the following code snippet produces the exception as well,


// iSL is a linkedlist
IEnumerator<int> e = iSL.GetEnumerator();
while(e.MoveNext()){
int i = e.Current;
Print(" " + this.pl(i2j(i)));
}

The problem is not consistent.

The following procedure solved the problem sometimes:

1) Close the strategy editor
2) Open the strategy editor
3) compile EXACTLY the same code that previously leads to error works sometimes!:eek:

NinjaTrader_Josh
10-06-2007, 03:47 AM
I don't believe .NET has a linked list by default. http://www.codeproject.com/csharp/doubly-linkedlist.asp

foreach should work fine. I used it in this (http://www.ninjatrader-support.com/vb/showthread.php?t=3476) indicator sample with no problems. I'm not sure which error window pop up you are referring to, but if you are seeing the same one I think you are seeing you can do this instead of restarting NinjaTrader.
Hit cancel on the pop up
Close NinjaScript editor
Reopen NinjaScript editor
F5 to compile
You will see meaningful error messages this timeThe error messages you should see because .NET doesn't have linked list are these:
The type or namespace name 'LinkedList' could not be found (are you missing a using directive or an assembly reference?)
foreach statement cannot operate on variables of type 'LinkedList<int>' because 'LinkedList<int>' does not contain a public definition for 'GetEnumerator'Edit: I see you've noticed the same solution to the NinjaScript editor bug. This is a persistent bug, but is very erratic and I have not been able to make any discernible pattern of what causes it yet. If you notice anything we would be glad to hear it.

shliang
10-06-2007, 04:25 AM
Thx for the prompt reply, Josh.

Though I'm not an expert, .NET does have a LinkedList implementation in the namespace System.Collections.Generic. I tried it in studio.net, it works well.

I got those error reporting under VMWare Fusion hosted on an iMac. I just rebooted my computer (an iMac) into windows XP. It seems I can't reproduce that problem.:confused:

Really confused!

NinjaTrader_Josh
10-06-2007, 04:29 AM
Ahh. Generic does the trick. Thanks for the info. The NinjaScript editor issue happens once in awhile. Majority of the times you won't experience it.

NinjaTrader_Dierk
10-06-2007, 05:22 AM
Suppose I have the following code snippet:

LinkedList<int> a = new LinkedList<int>();
a.AddLast(1);
foreach(int i in a){
}

A window pops up at compilation because of the foreach statement. But then even the foreach is removed, NinjaTrader cannot compile the strategy. Only a restart can solve this problem.

Here's what I don't understand:

1) foreach works well in .NET on LinkedList, why not in NinjaTrader?

2) why I have to restart NinjaTrader because of a bug or mis-use of keywords in my program? (If Visual Studio .NET does this, I'd short MSFT!)

How about "including" the appropriate namespace before using the generics?

Please try adding
using System.Collections.Generic; on top of your strategy file.

FREEN
02-11-2011, 03:08 AM
Borrowing this thread for a related question;

foreach loop gives error even though Iīve been adding the System.Collections.Generic namespace.

Code;

foreach (int bars in HighestbarsDS)
{
if (bars != 0)
{
PeaksDS.Set(bars);
}
}

Error message;

foreach statement cannot operate on variables of type 'NinjaTrader.Data.DataSeries' because 'NinjaTrader.Data.DataSeries' does not contain a public definition for 'GetEnumerator'

Why canīt the foreach loop access the HighestbarsDS dataseries array? Any suggestion on how to resolve this?

NinjaTrader_Bertrand
02-11-2011, 03:25 AM
FREEN, the dataseries do not implement an IEnumerable interface...try looping trough them the regular way.