2015-11-06 52 views
0

我正在嘗試修復這個錯誤,以便我可以在內部聲明的列表「學校」中的4個實例中啓動我的程序。然後我想允許用戶按照他們的意願操作列表。直到我添加了「Myfour」這個方法之前,所有東西都在運行。我如何調用這個函數以防止堆棧溢出錯誤出現?我是編程和C#的新手,今天需要完成這個任務。由方法調用引起的堆棧溢出錯誤

本公司主營:

using System; 

namespace Lab4 
{ 
    class MainClass 
    { 
     public static void Main (string[] args) 
     { 

      Console.WriteLine ("Welcome to the Course Monitoring System v1.0"); 
      Course myCourse = new Course(); 
      myCourse.Myfour(); 
      myCourse.Print(); 
      Course.Menu(); 
      myCourse.Print(); 
      myCourse.Choice(); 
     } 
    } 
} 

課程類別:

using System; 
using System.Collections.Generic; 

namespace Lab4 
{ 
    public class Course 
    { 
     private string courseNumber; 
     private string courseName; 
     private int courseHours; 
     public List<Course> school = new List<Course>(); 
     private int howmany=0; 
     private int totalcourse=0; 

     //This section is for returning and adjusting values of private data through the main program. 
     public string cn{ 
      get {return courseNumber; } 
      set {if (value != null) 
       cn = value; 
      } 
     } 
     public string name{ 
      get{ return courseName; } 
      set{if (value!="") 
       name=courseName; 
      } 
     } 
     public int hours{ 
      get {return courseHours; } 
      set {if (value != 0) 
       hours = value; 
      } 
     } 

     public Course() 
     { 
     } 
     public Course (string name, string cn, int hours) 
     { 
      courseNumber = cn; 
      courseName = name; 
      courseHours = hours; 
     } 

     //This portion of code overrides the string and allows it to output the information held within the constructor. 
     public override string ToString() 
     { 
      return courseNumber+" " + courseName + " " +" It is a " + courseHours + " hour course."; 
     } 
     //The menu application for my program 
     public static void Menu() 
     { 
      Console.WriteLine ("Please choose from one of the following options: "); 
      Console.WriteLine ("......................................................................"); 
      Console.WriteLine ("1.)Start a new Course List."); 
      Console.WriteLine ("2.)Delete Course from Current List."); 
      Console.WriteLine ("3.)Print Current Course List."); 
      Console.WriteLine ("4.)Add Course to Current List."); 
      Console.WriteLine ("5.)Shutdown Program"); 
     } 
     //This is the controller for the menu. It allows for functionality to control the tasks requested by the user. 
     public void Choice() 
     { 
      int selection=int.Parse(Console.ReadLine()); 
      while (selection >5 || selection < 1) { 
       Console.WriteLine ("Choice invalid. Please choose between 1 and 5."); 
       selection = int.Parse (Console.ReadLine()); 
      } 
      if (selection == 4) { 
       Add(); 
       Menu(); 
       Choice(); 
       Console.WriteLine(); 
      } 
      if (selection == 2) { 
       Delete(); 
       Menu(); 
       Choice(); 
       Console.WriteLine(); 
      } 
      if (selection == 3) { 
       Print(); 
       Menu(); 
       Choice(); 
       Console.WriteLine(); 
      } 
      if (selection == 1) { 
       New(); 
       Menu(); 
       Choice(); 
       Console.WriteLine(); 
      } 
      if (selection == 5) { 
       Console.WriteLine(); 
       Console.WriteLine ("Thank you for using this program for your scheduling needs."); 
      } 
     } 
     //This method when called will print a numbered list of courses refrenced by the created List 
     public void Print() 
     { 
      Console.WriteLine(); 
      Console.WriteLine ("Course Name.........Course Number.........Course Description........Course Hours"); 
      Console.WriteLine ("********************************************************************************"); 
      for (int i = 0; i < totalcourse; i++) { 
       int place = i + 1; 
       Console.WriteLine (place+".)"+school [i]); 
      } 
      Console.WriteLine(); 
     } 
     //This method will add an item to the end of the current list. 
     public void Add() 
     { 
      Console.WriteLine(); 
      Console.Write ("How many courses would you like to add at the end of your index?"); 
      int numberAdded = int.Parse(Console.ReadLine()); 
      for (int i = 0; i < numberAdded; i++) { 
       Console.WriteLine ("Please use the following templet for your entries..."); 
       Console.WriteLine ("Course Prefix, Course Name, Course Number, Course Hours, Course Description "); 
       school.Add (new Course (courseName = Console.ReadLine(), courseNumber = Console.ReadLine(), courseHours = int.Parse (Console.ReadLine()))); 
      } 
      totalcourse = totalcourse + numberAdded; 
      Console.WriteLine(); 
     } 
     //This method will delete an Item from the list based on the position 0-x based on x-1, the output that is seen by the user. After each iteration it will 
     //also create a list so that further deletions can be managed approiatly. 
     public void Delete() 
     { 
      if (totalcourse < 1) { 
       Console.WriteLine(); 
       Console.WriteLine ("There is nothing to delete!"); 
       Console.WriteLine(); 
      }else{ 
       Console.WriteLine(); 
       Console.Write ("How many entries do you wish to remove?"); 
       int removed = int.Parse (Console.ReadLine()); 
       for (int i = 0; i < removed; i++) { 
        Console.WriteLine ("Please type the index line number of the item you wish to remove."); 
        int delete = int.Parse (Console.ReadLine()); 
        school.RemoveAt (delete - 1); 
        totalcourse = totalcourse - 1; 
        Print(); 
       } 
      } 
      Console.WriteLine(); 
     } 
     //This method is called to create a new list of Courses to be created by the user. 
     public void New() 
     { 
      Console.WriteLine(); 
      if (howmany > 0) { 
       Clear(); 
      } else { 
       Console.Write ("How many courses do you want to create? "); 
       howmany = int.Parse (Console.ReadLine()); 
       Console.WriteLine(); 
       for (int i = 0; i < howmany; i++) { 
        Console.WriteLine(); 
        Console.WriteLine ("Please use the following templet for your entries..."); 
        Console.WriteLine ("Course Prefix, Course Name, Course Number, Course Hours, Course Description "); 
        school.Add (new Course (courseName = Console.ReadLine(), courseNumber = Console.ReadLine(), courseHours = int.Parse (Console.ReadLine()))); 
       } 
       totalcourse = totalcourse + howmany; 
       Console.WriteLine(); 
      } 
     } 
     //If there is already a list in place this method will be called and you will be asked if you wish to delete and start new. 
     public void Clear() 
     { 
      Console.WriteLine(); 
      Console.Write ("You want to discard old work and start a new list? Enter True or False...:"); 
      bool clean=bool.Parse(Console.ReadLine()); 
      if (clean == true) { 
       school.Clear(); 
       totalcourse = 0; 
       howmany = 0; 
       New(); 
      } else 
       Console.WriteLine ("No changes will be made. Exiting to Main Menu..."); 
      Console.WriteLine(); 
     } 
//This is the method to call the 4 predefined Courses 
//----->This is the part of my code that is causing the stack overflow error that I cant figure out... 
     public void Myfour() 
     { 
      Console.WriteLine ("These are four pre loaded courses."); 
      Course c1=new Course(name="Programming",cn="1101",hours=3); 
      Course c2=new Course(name="Algebra",cn="1101",hours=4); 
      Course c3=new Course(name="Chemistry 1",cn="1101",hours=4); 
      Course c4=new Course(name="Chemistry 1 Lab",cn="1101",hours=3); 
      school.Add (c1); 
      school.Add (c2); 
      school.Add (c3); 
      school.Add (c4); 

     } 
    } 
} 
+0

Can a Course can child courses?可能不是,但這就是你的代碼設置的。這個'school'變量不應該在Course類中。菜單看起來不屬於該類內部。 – LarsTech

回答

2

堆棧溢出是由遞歸地分配屬性引起的。你不應該這樣寫:

public string cn{ 
    get {return courseNumber; } 
    set {if (value != null) 
     cn = value; 
    } 
} 

看到cn又被分配了嗎?您應該使用像這樣的私有變量來更改該行:

public string cn{ 
    get {return courseNumber; } 
    set {if (value != null) 
     courseNumber = value; 
    } 
} 

對其他屬性執行相同操作。還要注意課程名稱中的錯誤。您應該使用值而不是courseName。這是應該的。

public string cn{ 
    get {return courseNumber; } 
    set {if (value != null) 
     courseNumber = value; 
    } 
} 
public string name{ 
    get { return courseName; } 
    set 
    { 
     if (value != "") 
      courseName = value; 
    } 
} 
public int hours{ 
    get {return courseHours; } 
    set {if (value != 0) 
     courseHours = value; 
    } 
} 
2

您必須爲自己的屬性:

public string cn 
{ 
    get { return courseNumber; } 
    set 
    { 
     if (value != null) 
      cn = value; 
    } 
} 

這將無限循環。您應該分配一些專用字段:

private string _cn; 
public string cn 
{ 
    get { return _cn; } 
    set 
    { 
     if (value != null) 
      _cn = value; 
    } 
}