NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > General Programming

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 10-06-2007, 03:29 AM   #1
shliang
Junior Member
 
Join Date: Sep 2007
Posts: 21
Thanks: 0
Thanked 0 times in 0 posts
Thumbs down Exception when compiling a strategy

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 is offline  
Reply With Quote
Old 10-06-2007, 03:43 AM   #2
shliang
Junior Member
 
Join Date: Sep 2007
Posts: 21
Thanks: 0
Thanked 0 times in 0 posts
Thumbs down

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!
shliang is offline  
Reply With Quote
Old 10-06-2007, 03:47 AM   #3
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

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 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.
  1. Hit cancel on the pop up
  2. Close NinjaScript editor
  3. Reopen NinjaScript editor
  4. F5 to compile
  5. You will see meaningful error messages this time
The 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.
NinjaTrader_Josh is offline  
Reply With Quote
Old 10-06-2007, 04:25 AM   #4
shliang
Junior Member
 
Join Date: Sep 2007
Posts: 21
Thanks: 0
Thanked 0 times in 0 posts
Default

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.

Really confused!
shliang is offline  
Reply With Quote
Old 10-06-2007, 04:29 AM   #5
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

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_Josh is offline  
Reply With Quote
Old 10-06-2007, 05:22 AM   #6
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Default

Quote:
Originally Posted by shliang View Post
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
Code:
using System.Collections.Generic;
on top of your strategy file.
NinjaTrader_Dierk is offline  
Reply With Quote
Old 02-11-2011, 03:08 AM   #7
FREEN
Senior Member
 
Join Date: Mar 2010
Location: Sweden
Posts: 123
Thanks: 6
Thanked 4 times in 3 posts
Default

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?
Last edited by FREEN; 02-11-2011 at 03:10 AM.
FREEN is offline  
Reply With Quote
Old 02-11-2011, 03:25 AM   #8
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,421
Thanks: 252
Thanked 982 times in 964 posts
Default

FREEN, the dataseries do not implement an IEnumerable interface...try looping trough them the regular way.
NinjaTrader_Bertrand is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling Code moflaherty Suggestions And Feedback 2 08-22-2007 07:23 PM
Compiler Error when compiling standard Ninja indicators MarlinTrader General Programming 1 05-25-2007 01:28 PM
Unhandled Exception, Strategy & Market Replay Connection californiaruby Automated Trading 5 02-20-2007 01:24 AM
Compiling NinjaScript Files - How does it work? NinjaTrader_Ray General Programming 0 01-24-2007 03:00 AM
Unhandled Exception? copodon Miscellaneous Support 1 03-08-2006 04:11 AM


All times are GMT -6. The time now is 04:23 PM.