1

I'm new to C# and programming all together and am trying to get this to run. I have spent the entire day reading about Static and non Static and cant seem to get it right. Any and all help would be appreciated as I need this code working by tomorrow evening. The following is the Main and Course Class:

using System;

namespace Lab4
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            Console.WriteLine ("Welcome to the Course Monitoring System v1.0");
            Course.Menu ();
            Course.Choice ();
        }
    }
}

This is the Course Class:

using System;
using System.Collections.Generic;

namespace Lab4
{
    public class Course
    {
        private string courseNumber;
        private string courseName;
        private int courseHours;
        private string descript;
        private string prefix;
        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 string script {
            get {return descript; }
            set {
                if (value != "")
                    script = value;
            }
        }
        public string pfix {
            get {return prefix; }
            set {if (value != "")
                pfix = value;
            }
        }

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

        //This portion of code overrides the string and allows it to output the information held within the constructor.
        public override string ToString ()
        {
            return prefix + " " + courseName+" " + courseNumber + " " + descript + " 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 (prefix = Console.ReadLine (), courseName = Console.ReadLine (), 
                    courseNumber = Console.ReadLine (), courseHours = int.Parse (Console.ReadLine ()), descript = 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 ();
                    school.Add (new Course (prefix = Console.ReadLine (), courseName = Console.ReadLine (), courseNumber = Console.ReadLine (), courseHours = int.Parse (Console.ReadLine ()), descript = 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 ();
        }
        /*  public static void Myfour ()
        {
            Console.WriteLine ();
            Console.WriteLine ("These are four pre loaded courses.");
            Course c1 = new Course ("MISSILE", 84, 3, "The Course is for missiles", "CRN");
            Console.WriteLine (c1);

            Console.WriteLine ();
        }*/
    }
}
Jason Harrod
  • 45
  • 1
  • 3
  • You have to make `Choice()` static as well to call it from a `static Main()` method. That will also need other methods called inside `Choice()` to be static. Or, create a new instance of `Course` class inside `Main()` and call the methods. – Arghya C Nov 06 '15 at 02:09
  • @ArghyaC he can't make `Choice` static without changing all the other methods/fields to static. – Ron Beyer Nov 06 '15 at 02:12
  • @RonBeyer that's what I have said. But yes, I have mentioned method, missed to mention fields. Thanks! – Arghya C Nov 06 '15 at 02:14
  • Thank you, but I've tried making Choice Static then I get the error on everything inside of Choice and I make those static then that continues until everything is Static. – Jason Harrod Nov 06 '15 at 02:15
  • When I make everything static it will compile, but when I enter choice 1 and create more than one input everything that gets put in prior to the last input is overwritten because everything is static and it doesn't allow for variables. – Jason Harrod Nov 06 '15 at 02:16
  • 1
    Looking at your code, easy way to go (without making lot of other changes that will actually make the code better) would be to create a new instance of `Course` class inside `Main()`. You can add a parameter less constructor for `Course`. – Arghya C Nov 06 '15 at 02:18
  • Try making all the functions called by Choice static, then make the school variable static – Goldorak84 Nov 06 '15 at 02:29

4 Answers4

1

Your Course class needs to be static:

public class Course

and so does your choice method:

public static void Choice()

Static means you can use members (e.g. a method) of a class without having to create an instance of the class using 'new'. This is what you're trying to do, so the method and container class both need the static modifier. You'll need to make all the other methods in your 'Course' class static as well though.

Or, why don't you just create an instance of your Course class in your Main method:

Course myCourse = new Course();
myCourse.Menu();
myCourse.Choice();

Then your code will work

Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206
0

This should get you started. What I have done is

  1. Made the constructor without arguments
  2. Created instance of Course in main method and invoke its methods

This is by no means complete but should get you started.

using System;
using System.Collections.Generic;
namespace Lab4
{
    public class MainClass
    {
        public static void Main (string[] args)
        {
            Console.WriteLine ("Welcome to the Course Monitoring System v1.0");
            var c = new Course();
            c.Menu();
            c.Choice();
        }
    }

    public class Course
    {
        private string courseNumber;
        private string courseName;
        private int courseHours;
        private string descript;
        private string prefix;
        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 string script {
            get {return descript; }
            set {
                if (value != "")
                    script = value;
            }
        }
        public string pfix {
            get {return prefix; }
            set {if (value != "")
                pfix = value;
            }
        }

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

        //This portion of code overrides the string and allows it to output the information held within the constructor.
        public override string ToString ()
        {
            return prefix + " " + courseName+" " + courseNumber + " " + descript + " It is a " + courseHours + " hour course.";
        }
        //The menu application for my program
        public 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 (prefix = Console.ReadLine (), courseName = Console.ReadLine (), 
                    courseNumber = Console.ReadLine (), courseHours = int.Parse (Console.ReadLine ()), descript = 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 ();
                    school.Add (new Course (prefix = Console.ReadLine (), courseName = Console.ReadLine (), courseNumber = Console.ReadLine (), courseHours = int.Parse (Console.ReadLine ()), descript = 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 ();
        }
        /*  public static void Myfour ()
        {
            Console.WriteLine ();
            Console.WriteLine ("These are four pre loaded courses.");
            Course c1 = new Course ("MISSILE", 84, 3, "The Course is for missiles", "CRN");
            Console.WriteLine (c1);

            Console.WriteLine ();
        }*/
    }
}
ndd
  • 3,051
  • 4
  • 25
  • 40
0

The first problem with your class is that the Course class contains a List<Course> school variable. A School is another logical entity and should be in another class. I think the Course class should be a class on it's own, without any static members. All the Console related functions and the school variable should be enclosed in a (maybe static, if that's a requirement) School class.

Your main function would then call

School.Menu();
School.Choice();
Goldorak84
  • 3,714
  • 3
  • 38
  • 62
0

The way you called Course.Choice() which is a className.methoddName() is meant for accessing a static method. That means you will need to change your

public void Choice() { }
public void Menu() { }

to

public static void Choice() { }
public static void Menu() { }

However, like I said before, since Choice() and Menu() are now static, therefore everything that is called within these methods have to be static as well, such as Add(), Delete(), Print().

So what does it mean to be a static method? - You don't (and can't) instantiate them to access them. The other approach to this, is to simply create an instance of class Course using the new keyword:

Course cr = new Course();
//This works because Course and MainClass are under the same namespace
//Or else, you will need to do this:
OtherNamespace.Class x = new OtherNamespace.Class() ;

and now you can access all the non-static methods in Course class! Like this:

cr.Menu();
cr.Choice();

Create an instance would seem to be an easier approach to solve your problem, because all of your methods are instance. But you should really understand when is more appropriate to use which. You can read more about that here.

Even though you only asked about calling non-static. I really think you have a lot of improvement to do on Course class. You clump too many things inside the class. For example, you can create a new class just to store all the properties like cn, name, hours, etc. Here it will be a good idea to set them as static property, then you can simply access them anywhere using TheNewClass.hours.

Community
  • 1
  • 1
Liren Yeo
  • 3,215
  • 2
  • 20
  • 41