Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

c# - StackOverflow Exception from get and set

I have the following code:

namespace QuantStrats
{
    class Program
    {
        static void Main(string[] args)
        {
            string FilePath = "C:\Users\files\DJ.csv";
            StreamReader streamReader = new StreamReader(FilePath);
            string line;
            List<Data> Data = new List<Data>();     

            while ((line = streamReader.ReadLine()) != null)
            {
                Data Tick = new Data();
                string [] values = line.Split(',');
                Tick.SetFields(values[1], values[2]);
                Data.Add(Tick);
            }

            for (int ii = 0; ii < Data.Count; ii++)
            {
                Data TickDataValues = new Data();
                TickDataValues = Data[ii];             
                Console.Write("TIME :" + TickDataValues.time + " Price : " + TickDataValues.price +  Environment.NewLine);
            }

            Console.ReadLine();
        }
    }

    class Data
    {
        public DateTime time
        {
            get { return this.time; }
            set
            {
                this.time = value;                
            }
        }

        public double price
        {
            get { return this.price; }
            set
            {
                this.price = value;                
            }
        }

        public void SetFields(string dateTimeValue, string PriceValue)
        {
            try
            {
                this.time = Convert.ToDateTime(dateTimeValue);
            }
            catch
            {
                Console.WriteLine("DateTimeFailed " + dateTimeValue + Environment.NewLine);
            }

            try
            {
                this.price = Convert.ToDouble(PriceValue);
            }
            catch
            {
                Console.WriteLine("PriceFailed " + PriceValue + Environment.NewLine);
            }
        }
    }
}

But I get a stack overflow exception.

I know it is because I am not doing my get and sets correctly and am entering an infinite loop, but I cannot see why exactly this is happening?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
public DateTime time
{
    get { return this.time; }
    set
    {
        this.time = value;                
    }
}

you aren't using backing fields, but setting the property itself from within the property setter.

You can fix this by using 1) an auto property

public DateTime Time { get; set; }

or 2) a backing field

private DateTime _time;
public Datetime Time 
{
    get { return _time; }
    set { _time = value; }
} 

they both equate to the same code.

For an explanation, when you get time in your code:

get { return this.time; } 

it has to retrieve the value of time to return. It does that by calling the get on time, which has to get retrieve the value of time, etc.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...