StackOverflowException
-
I get a StackOverFlowException when I try to assign values in an array to properties in a struct. Not sure what is going on, any one have any ideas? Here is the code: //Main Class public class BloombergEq { TradeData td = new TradeData(); public BloombergEq() { } public ArrayList ExecuteTradeData() { string fileName = @"D:\CSharp\file2.txt"; ArrayList rows = new ArrayList(); try { FileStream bbFile = new FileStream(fileName, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(bbFile); //Move the file cursor down 4 lines to get to start of data sr.ReadLine(); sr.ReadLine(); sr.ReadLine(); sr.ReadLine(); while (sr.Read() != -1) { object[] tokens = new object[12]; int tokenIndex = 0; string row = sr.ReadLine(); Regex r = new Regex("\\s(?=([^\"]*\"[^\"]*\")*(?![^\"]*\"))"); //matches all white space characters //splits line at every white space //disregards whitespaces & saves non-whitespace char for (int i=0; i<=r.Split(row).Length-1;i++) { if(r.Split(row).GetValue(i).Equals("")) { continue; } else { tokens[tokenIndex] = r.Split(row).GetValue(i); tokenIndex++; } } // for Console.WriteLine(tokens[0]); THIS IS WHERE THE ERROR IS THROWN td.orderTime = tokens[0]; td.transactionType = (string)tokens[1]; td.ticker = (string)tokens[3]; td.price = (float)tokens[4]; td.fillQ = (float) tokens[6]; td.account = (string)tokens[7]; td.broker = (string)tokens[8]; td.status = (string) tokens[9]; rows.Add(td); } //while } catch (Exception e) { Console.WriteLine(e); } return rows; } //ExecuteTradeData public static void Main(string[] args) { BloombergEq eq = new BloombergEq(); eq.ExecuteTradeData(); } } //BloombergEq //struct public struct TradeData { public object orderTime { get { return orderTime; } set { orderTime = value; } } public string transactionType { get { return transactionType; } set { transactionType = value; } } public string ticker { get { return ticker; } set { ticker = value; } } public float price { get { return price; } set { price = Convert.ToSingle(value)
-
I get a StackOverFlowException when I try to assign values in an array to properties in a struct. Not sure what is going on, any one have any ideas? Here is the code: //Main Class public class BloombergEq { TradeData td = new TradeData(); public BloombergEq() { } public ArrayList ExecuteTradeData() { string fileName = @"D:\CSharp\file2.txt"; ArrayList rows = new ArrayList(); try { FileStream bbFile = new FileStream(fileName, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(bbFile); //Move the file cursor down 4 lines to get to start of data sr.ReadLine(); sr.ReadLine(); sr.ReadLine(); sr.ReadLine(); while (sr.Read() != -1) { object[] tokens = new object[12]; int tokenIndex = 0; string row = sr.ReadLine(); Regex r = new Regex("\\s(?=([^\"]*\"[^\"]*\")*(?![^\"]*\"))"); //matches all white space characters //splits line at every white space //disregards whitespaces & saves non-whitespace char for (int i=0; i<=r.Split(row).Length-1;i++) { if(r.Split(row).GetValue(i).Equals("")) { continue; } else { tokens[tokenIndex] = r.Split(row).GetValue(i); tokenIndex++; } } // for Console.WriteLine(tokens[0]); THIS IS WHERE THE ERROR IS THROWN td.orderTime = tokens[0]; td.transactionType = (string)tokens[1]; td.ticker = (string)tokens[3]; td.price = (float)tokens[4]; td.fillQ = (float) tokens[6]; td.account = (string)tokens[7]; td.broker = (string)tokens[8]; td.status = (string) tokens[9]; rows.Add(td); } //while } catch (Exception e) { Console.WriteLine(e); } return rows; } //ExecuteTradeData public static void Main(string[] args) { BloombergEq eq = new BloombergEq(); eq.ExecuteTradeData(); } } //BloombergEq //struct public struct TradeData { public object orderTime { get { return orderTime; } set { orderTime = value; } } public string transactionType { get { return transactionType; } set { transactionType = value; } } public string ticker { get { return ticker; } set { ticker = value; } } public float price { get { return price; } set { price = Convert.ToSingle(value)
your problem is the way you wrote the properties. //struct public struct TradeData { public object orderTime { get { return orderTime; } set { orderTime = value; } } this is a c# translation of the resulting IL created for that: public Object get_orderTime() { return get_orderTime(); } public set_orderTime(Object value) { set_orderTime(value); } both of these will obviously recurse until you stackoverflow. either make those properties into fields, or provide internal storage for the public property.
-
your problem is the way you wrote the properties. //struct public struct TradeData { public object orderTime { get { return orderTime; } set { orderTime = value; } } this is a c# translation of the resulting IL created for that: public Object get_orderTime() { return get_orderTime(); } public set_orderTime(Object value) { set_orderTime(value); } both of these will obviously recurse until you stackoverflow. either make those properties into fields, or provide internal storage for the public property.
I looked earlier for something like that, but just couldnt see it. 10 points for you :) leppie::AllocCPArticle(Generic DFA State Machine for .NET);
-
your problem is the way you wrote the properties. //struct public struct TradeData { public object orderTime { get { return orderTime; } set { orderTime = value; } } this is a c# translation of the resulting IL created for that: public Object get_orderTime() { return get_orderTime(); } public set_orderTime(Object value) { set_orderTime(value); } both of these will obviously recurse until you stackoverflow. either make those properties into fields, or provide internal storage for the public property.
-
Thanks for the response Andy. What do you mean by providing internal storage for public property? Thanks again.
something like this: public struct TradeData { public object OrderTime { get { return orderTime; } set { orderTime = value; } } private Object orderTime; }