Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. StackOverflowException

StackOverflowException

Scheduled Pinned Locked Moved C#
questioncsharpdata-structuresregexhelp
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zuhx
    wrote on last edited by
    #1

    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)

    A 1 Reply Last reply
    0
    • Z zuhx

      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)

      A Offline
      A Offline
      Andy Smith
      wrote on last edited by
      #2

      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.

      L A 2 Replies Last reply
      0
      • A Andy Smith

        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.

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        I looked earlier for something like that, but just couldnt see it. 10 points for you :) leppie::AllocCPArticle(Generic DFA State Machine for .NET);

        1 Reply Last reply
        0
        • A Andy Smith

          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.

          A Offline
          A Offline
          Anonymous
          wrote on last edited by
          #4

          Thanks for the response Andy. What do you mean by providing internal storage for public property? Thanks again.

          A 1 Reply Last reply
          0
          • A Anonymous

            Thanks for the response Andy. What do you mean by providing internal storage for public property? Thanks again.

            A Offline
            A Offline
            Andy Smith
            wrote on last edited by
            #5

            something like this: public struct TradeData { public object OrderTime { get { return orderTime; } set { orderTime = value; } } private Object orderTime; }

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups