Getting List of objects of same class

I have a class ‘Task’ which represents any Task that is done by an individual employee. For this class I wrote properties like EmpID, Priority, etc. and methods like AddNewTask(), ArchiveTask(), MoveTaskToUser(int userid), etc.
Then I wrote a method

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public List<Task> GetAllTasks()
</code>
<code>public List<Task> GetAllTasks() </code>
public List<Task> GetAllTasks()

This seemed incorrect to me with regard to OOP. I thought any single task object should represent one single task and it doesnt seem right to call taskObj.GetAllTasks() because object taskObj should carry out methods on the single Task that it represents.
So I changed the method to static:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public static List<Task> GetAllTasks()
</code>
<code>public static List<Task> GetAllTasks() </code>
public static List<Task> GetAllTasks()

Now I call Task.GetAllTasks(). Is this the proper way to follow object oriented approach. Was the earlier idea of calling the method from single object correct?

The static approach is a good start. You could also just write up a class that manages all tasks. You could then go like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>TaskManager.addTask(/*whatever params are necessary*/);
TaskManager.getTask(/*whatever identifier you desire*/);
TaskManager.getTasksOfEmployer(/*employer-id*/);
TaskManager.getAllTasks();
TaskManager.updateTask();
etc...
</code>
<code>TaskManager.addTask(/*whatever params are necessary*/); TaskManager.getTask(/*whatever identifier you desire*/); TaskManager.getTasksOfEmployer(/*employer-id*/); TaskManager.getAllTasks(); TaskManager.updateTask(); etc... </code>
TaskManager.addTask(/*whatever params are necessary*/);
TaskManager.getTask(/*whatever identifier you desire*/);
TaskManager.getTasksOfEmployer(/*employer-id*/);
TaskManager.getAllTasks();
TaskManager.updateTask();
etc...

9

I have done something similar in the past and I used a TaskList type of object which contained the list of Task objects and manages the addition, removal and retrieval of each of the Tasks. Each Task implemented an ITask interface which had a Name property which was used to retrieve a task.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public interface ITask
{
string Name { get;set; }
void Execute();
}
</code>
<code>public interface ITask { string Name { get;set; } void Execute(); } </code>
public interface ITask
{
    string Name { get;set; }
    void Execute();
} 

Each task would implement this interface and as such the class that manages the Tasks does not need to know about any particular, it just knows it can call the Execute method and a Task will carry out its action.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class TaskList
{
private List<ITask> _taskList = new List<ITask>();
public void Add(ITask task)
{
//Check for null / duplicate task
_taskList.Add(task);
}
public void GetTask(string name)
{
foreach(var task in _taskList)
{
if(task.Name.Equals(name))
return task;
}
throw new ArgumentOutOfRangeException();
}
public void Remove(ITask task)
{
if(_taskList.Contains(task))
_taskList.Remove(task);
}
}
</code>
<code>public class TaskList { private List<ITask> _taskList = new List<ITask>(); public void Add(ITask task) { //Check for null / duplicate task _taskList.Add(task); } public void GetTask(string name) { foreach(var task in _taskList) { if(task.Name.Equals(name)) return task; } throw new ArgumentOutOfRangeException(); } public void Remove(ITask task) { if(_taskList.Contains(task)) _taskList.Remove(task); } } </code>
public class TaskList
{
    private List<ITask> _taskList = new List<ITask>();
    public void Add(ITask task)
    {
        //Check for null / duplicate task
        _taskList.Add(task);
    }

    public void GetTask(string name)
    {
        foreach(var task in _taskList)
        {
            if(task.Name.Equals(name))
                return task;
        }
        throw new ArgumentOutOfRangeException();
    }

    public void Remove(ITask task)
    {
        if(_taskList.Contains(task))
            _taskList.Remove(task);            
    }
}

Hope this helps a bit.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật