Click here to Skip to main content
15,886,632 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is probably a very simple and basic object oriented design topic, but ..., well, I have not done that before and I could not find a suitable solution on the Internet.

How do you create a class which has a collection or array as one of its properties?

For example, if I have the following classes:
- Student, for an individual student
- Students, for a group of students (the collection class)
- Workshop, for a workshop which may have several students

How should I design the Workshop class? Should it have a Students collection or should it have an array of student classes?

Whichever recommendation you provide, would you please provide sample code for the Workshop class and also from consumer of the Workshop class on adding and manipulating the "array" or the "collection" of students?

I already know how to develop a Collection class, but I have difficulty adding the collection as a property into the Workshp class.


Thanks.
Posted
Updated 20-Jun-11 9:35am
v3

I would do it this way:

public class Student
{
}

public class Workshop
{
    public List<student> Students {get; private set;}

    public Workshop()
    {
        Students = new List<student>();
    }

}</student></student>



This is a safer way to implement. Avoids null reference exception.
 
Share this answer
 
Comments
Member 7939131 20-Jun-11 17:59pm    
Vivek I got this to work. Thank you very much.
Sergey Alexandrovich Kryukov 20-Jun-11 23:58pm    
That's it, my 5.
--SA
Vivek Krishnamurthy 21-Jun-11 2:21am    
Thanks.. :)
C#
public class Student
{
}

public class Roster : List<Student>
{
}

public class Workshop : Roster
{
}

// or

public class Workshop
{
    Roster roster = new Roster();
}


I like to create a class that inherits from List because it's cleaner when you use it later (and you don't have to remember the type of object the list contains). This:

C#
Roster roster = new Roster()
public Roster GetRoster(){}

is much cleaner than this:

C#
List>Student< roster = new List<Student>()
public List<Student> GetRoster(){}
 
Share this answer
 
v2
You can have two classes -
class Student
{
   //Some properties
}

//Wirk shop class containing a Students collection
class WorkShop
{
   List<student> Students {get;set;}
}
 
Share this answer
 
v2
Comments
#realJSOP 20-Jun-11 13:03pm    
Fixed your tags. :)
Abhinav S 21-Jun-11 0:23am    
Thanks :).
Member 7939131 20-Jun-11 17:44pm    
/*
Hi John, I tried using this option in a Console App, and when I tried to consume the object, I have the following exception:

Object reference not set to an instance of an object.

Please help


*/
// Here are the classes:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Student
{
public int Id { get; set; }
public string StudentName {get ; set;}
}

class Workshop
{
public string TeacherName { get; set; }
public List<student> Students {get;set;}
}
}


// And here is the main of the Console app:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Workshop w = new Workshop();

Student a = new Student { Id = 5, StudentName = "First Student" };
w.Students.Add(a); // <----------------- Got the exception here.

Student b = new Student { Id = 15, StudentName = "Second Student" };
w.Students.Add(b);

Student c = new Student { Id = 25, StudentName = "Third Student" };
w.Students.Add(c);

Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);


}
}
}
Member 7939131 20-Jun-11 18:02pm    
John,,, fyi...I got Solution 4 below to work.
You can do this:
public class Student
{
    // do stuff here
}
public class Students : List<student>
{
    // do stuff here
}
public class Workshop
{
    Students myStudents = new Students ();
    // do other stuff here
}
</student>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900