1

I am learning how to use Interface of C#, I reading this artcile : http://www.codeproject.com/Articles/286863/I-Interface

One of the example:

 public class TaskComposite : ITask
{
 public TaskComposite(params ITask[] tasks)
 {
 this.composedTasks = new List<ITask>();
 if(tasks != null)
 {
 foreach(var task in tasks)
 AddTask(task);
 }
 }
 public void Do()
 {
 foreach(var task in this.composedTasks)
 task.Do();
 }
 public void AddTask(ITask task)
 {
 if(task != null)
 this.composedTasks.Add(task);
 }
 private List<ITask> composedTasks;
} 

The description said:

we can group a load of tasks together to be executed serially.

I create a Windows form as create a new instance of TaskComposite class, But what should i do next?

public partial class FormInterface : Form
{
 public FormInterface()
 {
 InitializeComponent();
 }
 private void FormInterface_Load(object sender, EventArgs e)
 {
 ITask[] TaskAry = new ITask[] { };
 TaskAry[0] = new TaskClass().Do(); //<=Error
 TaskComposite tc = new TaskComposite(TaskAry);
 }
 class TaskClass : ITask
 {
 public void Do()
 {
 Console.WriteLine(DateTime.Now.ToString());
 }
 }
}
Erik Philips
54.8k11 gold badges132 silver badges158 bronze badges
asked May 11, 2012 at 7:04
0

4 Answers 4

2

You have several mistakes here.

First, when you create an array, you should specify its size, or initialize it. This is a difference between collections (which has variable size) and arrays. So, you should provide size

ITask[] TaskAry = new ITask[1]; 
TaskAry[0] = new TaskClass();

Or initialize (size will be computed by compiler):

ITask[] TaskAry = new ITask[] { new TaskClass() }; 

But in your case, when params ITask[] tasks is used, you can simply pass list of tasks:

TaskComposite tc = new TaskComposite(new TaskClass());

Pattern Composite allows you to treat group of objects as single object. In your case composite task here to allow you treat several task as one task object. So, without composite task, you'll need to use array/collection of tasks and execute each of them manually. Like this:

ITask[] tasks = new ITask[] { new TaskClass(), new TaskClass() };
foreach(ITask task in tasks)
 task.Do();

With composite tasks you don't need to iterate over all tasks:

ITask compositeTask = new TaskComposite(new TaskClass(), new TaskClass());
compositeTask.Do(); // executes all tasks

You can pass composite task where simple ITask is expected. And no one will know that there is actually a bunch of tasks.

answered May 11, 2012 at 7:41
Sign up to request clarification or add additional context in comments.

1 Comment

@SilverNight no problem :) Btw keep in mind, that you use Console in winforms application. By default you will not see any output of TaskClass.
2

Rather Do Something Like This:-

private void FormInterface_Load(object sender, EventArgs e)
{
ITask[] TaskAry = new[]{ new TaskClass() };
TaskComposite tc = new TaskComposite(TaskAry);
tc.Do(); //This Will Start Your Task, In Case you Want To Start It Here.
}
class TaskClass : ITask
{
public void Do()
{
 Console.WriteLine(DateTime.Now.ToString());
}
}
answered May 11, 2012 at 7:14

Comments

2

Initialize the array and the method Do returns void

// wrong
ITask[] TaskAry = new ITask[] { }; 
TaskAry[0] = new TaskClass().Do();
// correct
ITask[] TaskAry = new ITask[] { new TaskClass() }; 
TaskAry[0].Do();
// better
// TaskComposite constructor accepts param array
TaskComposite tc = new TaskComposite(new TaskClass()); 
tc.Do(); // execute passed instance of TaskClass
answered May 11, 2012 at 7:12

Comments

1

Because you are not specifying the size inside

 ITask[] TaskAry = new ITask[10]; //Specify the size or specify a value in {}
 TaskAry[0] = new TaskClass(); //Also this interface may hold an object of TaskClass
 TaskComposite tc = new TaskComposite(TaskAry);
answered May 11, 2012 at 7:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.