tags:

views:

487

answers:

4

I have a C# class to store my User details and another for storing JOBS details.The scenario is like each User can have multiple JOBS. I have UserId,UserName,Age etc as my User class properties. Now i want to associate the of JOBS class objects to a property called JOBS so that i can store multiple jobs associated with this user to that as a List. How to write the Property in my USerClass ? .How should be the set and get ?

+3  A: 
public class User
{
 private List<Jobs> m_JobList = new List<Jobs>();

 public List<Jobs> JobList
 {
  get { return m_JobList; }
 }
}

try something like this.

astander
+1  A: 

If you want to depend upon abstractions

public interface IUser
{
    IList<IJob> Jobs { get; set; }
}

public class User : IUser
{
    IList<IJob> Jobs { get; set; }  // Automatic properties C# >= 3

    public User(IList<IJob> jobs)
    {
        Jobs = jobs; 
    }
}

Replace IList with ICollection if you don't need indexing and just want a count and be able to enumerate through the jobs. Also consider ReadOnlyCollection<IJob> if a user can't change jobs (immutable), by removing set; from interface and replace set; with private set; in the class.

Si
This will be difficult to serialize, as interfaces can't, in general, be serialized.
John Saunders
No mention of this in the question, but you could declare [XmlInclude(typeof(Job))], or implement IXmlSerializer for full control (not hard or much code), or have a simple wrapper/DTO. It all depends on expected usage as to how far you need/want to go.
Si
A: 

You can also use ArrayList as the following:

public class User
{
private ArrayList m_Jobs = new ArrayList();

 public ArrayList Jobs
 {
  get {return m_Jobs;}
 }
}
Wael Dalloul
I would really advice against using `ArrayList` these days, since it does not provide any form of type safety. `List<T>` it a much better choice.
Fredrik Mörk
-1: The advice of Arraylist when you KNOW that the property will be of type n*Job is at least weird.
flq
-1 for using ArrayList. Even if you don't know the type of each item, `List<object>` would be better. `ArrayList` is a relic of .NET 1.1, before generics existed.
John Saunders
A: 

Another two thoughts; you can use C# 3.0 auto props if you like, but you need to use the constructor to do the initial set:

class User {
    public List<Job> Jobs {get; private set;}
    public User() {
        Jobs = new List<Job>();
    }
}

Also; if you are using xml serialization via XmlSerializer, the property (annoyingly) needs to have a (public) setter (regardless of whether you use auto-props or an explicit field); much gnashing of teeth over this (also, the class must be public):

public class User {
    public List<Job> Jobs {get; set;}
    public User() {
        Jobs = new List<Job>();
    }
}
Marc Gravell
@Marc: how do you feel about the FxCop warning this will generate, about returning `Collection<T>` instead of `List<T>`?
John Saunders
Frankly, to hell with it. There are such things as unnecessary abstractions... I'd be happy with `IList<T>` etc, but that won't work with many serializers.
Marc Gravell