473,385 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

I don't understand why I am getting a StackoverFlowException??

I am new to C Sharp, and have been working on this code for a few days. I keep getting a StackoverFlowException and I don't know what that means, since we haven't covered it in class. Any guidance would be greatful. Thanks!

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. public class BookDemo
  3. {
  4.     public static void Main()
  5.     {
  6.         Book Book = new Book();
  7.         TextBook Text = new TextBook();
  8.         CoffeeTableBook Coffee = new CoffeeTableBook();
  9.  
  10.         Book.Isbn = "0001112223330 ";
  11.         Book.Title = "The Story of Day of the Dead";
  12.         Book.Author = "Victor Juan";
  13.         Book.Price = 99.95;
  14.  
  15.         Book.Isbn = "2323565678 ";
  16.         Book.Title = "The Adventure guide";
  17.         Book.Author = "Steven Chard";
  18.         Book.Price = 55.00;
  19.  
  20.         Text.Isbn = "1113334444569 ";
  21.         Text.Title = "Computers, How they Work";
  22.         Text.Author = "John Smith";
  23.         Text.GradeLevel = 10;
  24.         Text.Price = 132.00;
  25.  
  26.         Text.Isbn = "9995558475890 ";
  27.         Text.Title = "The Fundementals of Math";
  28.         Text.Author = "Marcia Brown";
  29.         Text.GradeLevel = 3;
  30.         Text.Price = 56.25;
  31.  
  32.         Coffee.Isbn = "1932279288 ";
  33.         Coffee.Title = "Household Baggage";
  34.         Coffee.Author = "Marna Krajeski";
  35.         Coffee.Price = 46.25;
  36.  
  37.         Coffee.Isbn = "0757300065 ";
  38.         Coffee.Title = "Chicken Soup for the Soul of America";
  39.         Coffee.Author = "Jack Canfield";
  40.         Coffee.Price = 125.75;
  41.  
  42.         Console.WriteLine("ISBN: {0}", Book.Isbn);
  43.         Console.WriteLine("  {1} by {2} sells for {3}",
  44.            Book.Title, Book.Author,
  45.            Book.Price.ToString("C"));
  46.  
  47.         Console.WriteLine("\nISBN: {0}", Text.Isbn);
  48.         Console.WriteLine("  {1} by {2} for grade {3} sells for {4}",
  49.            Text.Title, Text.Author,
  50.            Text.GradeLevel,
  51.            Text.Price.ToString("C"));
  52.  
  53.         Console.WriteLine("\nISBN: {0}", Coffee.Isbn);
  54.         Console.WriteLine("  {1} by {2} sells for {3}",
  55.            Coffee.Title, Coffee.Author,
  56.            Coffee.Price.ToString("C"));
  57.     }
  58. }
  59. public class Book
  60. {
  61.     public string Isbn { get; set; }
  62.     public string Title { get; set; }
  63.     public string Author { get; set; }
  64.     public double Price { get; set; }
  65. }
  66. public class TextBook : Book
  67. {
  68.     private int gradeLevel;
  69.     const double LOWPRICE = 20.00;
  70.     const double HIGHPRICE = 80.00;
  71.     public int GradeLevel
  72.     {
  73.         get
  74.         {
  75.             return gradeLevel;
  76.         }
  77.         set
  78.         {
  79.             gradeLevel = value;
  80.         }
  81.     }
  82.     new public double Price
  83.     {
  84.         get
  85.         {
  86.             return Price;
  87.         }
  88.         set
  89.         {
  90.             if (value < LOWPRICE)
  91.                 Price = LOWPRICE;
  92.             else if (value > HIGHPRICE)
  93.                 Price = HIGHPRICE;
  94.             else Price = value;
  95.         }
  96.     }
  97. }
  98. public class CoffeeTableBook : Book
  99. {
  100.     public const double LOWPRICE = 35.00;
  101.     public const double HIGHPRICE = 100.00;
  102.     new public double Price
  103.     {
  104.         get
  105.         {
  106.             return Price;
  107.         }
  108.         set
  109.         {
  110.             if (value < LOWPRICE)
  111.                 Price = LOWPRICE;
  112.             else if (value > HIGHPRICE)
  113.                 Price = HIGHPRICE;
  114.             else Price = value;
  115.         }
  116.     }
  117. }
Nov 18 '10 #1
4 1553
hype261
207 100+
A StackOverFlowException is thrown for execution stack overflow errors, typically in case of a very deep or unbounded recursion.

My question is does your code compile? The only obvious error that I see is the line.

Book Book = new Book();

You are using a class name as a variable name.
Nov 18 '10 #2
No my program does not compile, when I go to debug it says the error and then highlights the first curly brace in the below method.

Expand|Select|Wrap|Line Numbers
  1. {
  2.    if (value < LOWPRICE)
  3.        Price = LOWPRICE;
  4.    else if (value > HIGHPRICE)
  5.        Price = HIGHPRICE;
  6.    else Price = value;
  7. }
  8.  
Nov 18 '10 #3
Joseph Martell
198 Expert 128KB
One common cause of a stack overflow is infinite recursion. Each function call pushes information onto the stack, so an infinite recursion keeps pushing data onto the stack until you get an overflow. That is what is happening in your case (I believe):

Expand|Select|Wrap|Line Numbers
  1. public class Book
  2. {
  3.     public string Isbn { get; set; }
  4.     public string Title { get; set; }
  5.     public string Author { get; set; }
  6.     public double Price { get; set; }
  7. }
  8. public class TextBook : Book
  9. {
  10.     private int gradeLevel;
  11.     const double LOWPRICE = 20.00;
  12.     const double HIGHPRICE = 80.00;
  13.     public int GradeLevel
  14.     {
  15.         get
  16.         {
  17.             return gradeLevel;
  18.         }
  19.         set
  20.         {
  21.             gradeLevel = value;
  22.         }
  23.     }
  24.     //You named this property "Price".  Every unqualified reference
  25.     //to "Price" in this class uses either the get or set
  26.     // of this property
  27.     new public double Price
  28.     {
  29.         get
  30.         {
  31.             //This is an infinitely recursive call to
  32.             //the Price property get method.
  33.             return Price;
  34.         }
  35.         set
  36.         {
  37.             if (value < LOWPRICE)
  38.                 Price = LOWPRICE;  //this accesses the set of the Price property of the TextBook class
  39.             else if (value > HIGHPRICE)
  40.                 Price = HIGHPRICE;  // so does this
  41.             else Price = value;  //so does this
  42.         }
  43.     }
  44. }
  45.  
  46. //you have the same recursive calls in this class as well
  47. public class CoffeeTableBook : Book
  48. {
  49.     public const double LOWPRICE = 35.00;
  50.     public const double HIGHPRICE = 100.00;
  51.     new public double Price
  52.     {
  53.         get
  54.         {
  55.             return Price;
  56.         }
  57.         set
  58.         {
  59.             if (value < LOWPRICE)
  60.                 Price = LOWPRICE;
  61.             else if (value > HIGHPRICE)
  62.                 Price = HIGHPRICE;
  63.             else Price = value;
  64.         }
  65.     }
  66. }
  67.  
  68.  
That is how you get infinite recursion. You can fix this by fully qualifying the Price you are using in your TextBook and CoffeeTableBook classes:

Expand|Select|Wrap|Line Numbers
  1. public class Book
  2. {
  3.     public string Isbn { get; set; }
  4.     public string Title { get; set; }
  5.     public string Author { get; set; }
  6.     public double Price { get; set; }
  7. }
  8. public class TextBook : Book
  9. {
  10.     private int gradeLevel;
  11.     const double LOWPRICE = 20.00;
  12.     const double HIGHPRICE = 80.00;
  13.     public int GradeLevel
  14.     {
  15.         get
  16.         {
  17.             return gradeLevel;
  18.         }
  19.         set
  20.         {
  21.             gradeLevel = value;
  22.         }
  23.     }
  24.     new public double Price
  25.     {
  26.         get
  27.         {
  28.             return base.Price;
  29.         }
  30.         set
  31.         {
  32.             if (value < LOWPRICE)
  33.                 base.Price = LOWPRICE;
  34.             else if (value > HIGHPRICE)
  35.                 base.Price = HIGHPRICE;
  36.             else base.Price = value;
  37.         }
  38.     }
  39. }
  40. public class CoffeeTableBook : Book
  41. {
  42.     public const double LOWPRICE = 35.00;
  43.     public const double HIGHPRICE = 100.00;
  44.     new public double Price
  45.     {
  46.         get
  47.         {
  48.             return base.Price;
  49.         }
  50.         set
  51.         {
  52.             if (value < LOWPRICE)
  53.                 base.Price = LOWPRICE;
  54.             else if (value > HIGHPRICE)
  55.                 base.Price = HIGHPRICE;
  56.             else base.Price = value;
  57.         }
  58.     }
  59. }
  60.  
  61.  
Nov 19 '10 #4
Compile your code first. Like hype is saying, Book can not be used as a var because its a class name.

And I dont know if this is OK:
"new public double Price"
Nov 19 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Bart | last post by:
Hi all I don't understand globals between multiple modules in a python program. I really don't. I've narrowed it down to the following two very simple programs a.py and b.py. When I run a.py I...
7
by: Robert Nicholson | last post by:
So I've got one page where I have an image inside a DIV with text-align: center and the image is correctly centered in that block. Making me think that text-align will center the contents of a...
5
by: Jesee | last post by:
I am reading Jeffrey Richter's book "Applied Microsoft .NET Framework programming",i came across "Exception handing". Page 405 says "If the stack overflow occurs within the CLR itself,your...
10
by: Wayne Wengert | last post by:
I am using a datareader (dr1) to read through rows returned from a query. I am getting the error: "No data exists for the row/column." at the "If IsDbNull..." in the code below. The field "Photo1"...
2
by: Lee Crabtree | last post by:
Okay, maybe I'm trying to use streams in some unacceptable way, but I'm running into a few problems. I need to be able to serialize a set of data and deserialize it in another place. It seems...
8
by: Joshua Moore | last post by:
/* Hi, I was hoping someone could help me with this problem. I did my work and worked my way through the usual compiler messages, but I have run against some problem I can't identify. The compiler...
4
by: Dave | last post by:
When and what does .Net want to compile? I have a file called upload.cs. I made changes to it. Tried to rebuild. But it won't saying "the application is already precomiled." I would have...
3
by: Ben Thomas | last post by:
Hello, I have the following code which I don't understand why it works : #include <iostream> using namespace std; void DontWork (unsigned int& i) { cout << i << endl; }
0
by: Stef Mientki | last post by:
Terry Reedy wrote: sorry, don't know how this happened, as I always copy/paste ? AFAIK locals() == sys._getframe(0).f_locals AFAIK, again one level up weird, I use it in 2.5 and if I remember...
5
by: Thierry | last post by:
Hello fellow pythonists, I'm a relatively new python developer, and I try to adjust my understanding about "how things works" to python, but I have hit a block, that I cannot understand. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.