-3

I'm making an attempt at looping through Course objects and displaying them to the console. For some reason however my solution doesn't seem to work, can anyone detect any obvious issues that I might be the cause for this? thanks :)

class Course
{
    private int v1;
    private string v2;
    private string v3;
    private int v4;

    public Course(int v1, string v2, string v3, int v4)
    {
        this.v1 = v1;
        this.v2 = v2;
        this.v3 = v3;
        this.v4 = v4;
    }

    public int courseCode { get; set; }
    public string courseName { get; set; }
    public string professor { get; set; }
    public int capacity { get; set; }
}

class Computation
{
   public static List<Course> courses = new List<Course>();

    public static void addCourse(Course c)
    {
        courses.Add(c);
    }

    public static void viewCourses()
    {
        foreach(var c in courses)
        {
            Console.WriteLine(c.courseName);
        }
    }
}

Main

 static void Main(string[] args)
         {
             Course computerScience = new Course(1, "Computer Science", "Dr Shkhla", 200);
             Course mathematics = new Course(2, "Mathematics", "Dr Shkhla", 200);
             Course physics = new Course(3, "Physics", "Dr Shkhla", 200);

             addCourse(computerScience);
             addCourse(mathematics);
             addCourse(physics);
             viewCourses();                 
         }
openshac
  • 4,966
  • 5
  • 46
  • 77
J Doe
  • 23
  • 3

3 Answers3

1

The problem is that you are never assigning anything to those properties. Also, it is a very good time to learn standard naming conventions:

public class Course
{
    public Course(int code, string name, string professor, int capacity)
    {
        Code = code;
        Name = name;
        Professor = professor;
        Capacity = capacity;
    }

    // properties should be in PascalCase
    // and should not have their entity's name as prefix 
    public int Code { get; set; }
    public string Name { get; set; }
    public string Professor { get; set; }
    public int Capacity { get; set; }
}

Names that are sequences (v1, v2, etc in your code) should be avoided most of the times.
Also, notice that I deleted the unused properties from the class.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
0

Change your constructor to:

public Course(int courseCode, string courseName, string professor, int capacity)
{
    this.courseCode = courseCode;
    this.courseName = courseName;
    this.professor = professor;
    this.capacity = capacity;
}
openshac
  • 4,966
  • 5
  • 46
  • 77
  • 1
    Or use correct naming conventions and avoid the horrible `this.` – Camilo Terevinto Mar 11 '18 at 15:44
  • I guess it's a matter of preference. StyleCop actually recommends using the keyword this. But I realise some people hate using it. https://stackoverflow.com/questions/1562540/why-does-stylecop-recommend-prefixing-method-or-property-calls-with-this – openshac Mar 12 '18 at 08:25
0

You need to update your constructor:

public Course(int v1, string v2, string v3, int v4)
{
    courseCode = v1;
    courseName = v2;
    professor = v3;
    capacity = v4;
}

or set your properties directly:

Course computerScience = new Course
{
  courseCode = 1,
  courseName  = "Computer Science",
  professor = "Dr Shkhla",
  capacity = 200
};

If you like to have an nicer console output, override your toString() function of the Course model.

public override string ToString()
{
    return $"Course {courseCode}: {courseName} by {professor}. Capacity: {capacity}";
}
DTeuchert
  • 523
  • 8
  • 19