Deriving from IList question

B

Bart

Hi all,

I have created a class which derives fron IList.

It looks like this:

public class Signals<Signal>: IList<Signal>

But I don't know how to impement the next section.

#region IEnumerable Members

System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}

#endregion

How should I implent this. Can someone give me an example ?

Thanks in advance,

Bart
 
B

Bart

Hi Peter,

Thanks for your quick answer.

In my class I create a private list. Can I just return _list.GetEnumerator()

Sorry for the probably dumb question :(

Bart


Peter Duniho said:
[...]
But I don't know how to impement the next section.

System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}

How should I implent this. Can someone give me an example ?

You probably are looking for something like this:
http://msdn2.microsoft.com/en-us/library/dscyy5s0.aspx

It documents implementation of iterators in C#.

Pete
 
M

Marc Gravell

For the record - alternative approaches might be to inherit from
Collection<T> or List<T> (the first offers more extension points) and
just add the functionality you need... something like below. This then
given you standard IList/IList<T> etc for free:

public sealed class Signals : Collection<Signal>
{
protected override void InsertItem(int index, Signal item)
{
CheckValid(item);
base.InsertItem(index, item);
}
protected override void SetItem(int index, Signal item)
{
CheckValid(item);
base.SetItem(index, item);
}
void CheckValid(Signal item)
{
if (item.Foo < 0) throw new InvalidOperationException("Foo
must be non-negative");
}
}
 
B

Bart

Another question about this.

I did try to derive from List<T> and Collection<T> but I was not able to
override for example the Add method. I am happy the way it is implemented it
right now.

But one thing I can't accomplish and could need some more help with it. In
the method add I want to show a messagebox with the signal name like this:

public void Add(Signal item)
{
MessageBox.Show(item.Name);
_list.Add(item);
}

But I can't compile this and I don't understand why item is not of type
Signal. Even casting it to a Signal didn't do the trick.

So can someone show me how I could to this.

Thanks again,

Bart
 
B

Bart

Note also that the Add() method isn't virtual, so you can't override it
anyway. If you have the need to provide your own behavior, it may in fact
be better to do this the way you were before, with composition rather than
inheritance.

That's exactly the reason why I choose for composition. Anyway the
constructor for my Signals get a reference to a validator. This validator
should validate the Signal item.

Sore here is the most important peace of the code I hope you could help me
out here. Thanks again.

public class SignalValidator
{
public bool Validate(Signal signal)
{
return true; // True for the moment !!!
}
}
-------------------------------------------------------
public class Signal
{
private int _number;
private string _name;
private string _comment;

public Signal()
{
}

public Signal(int Number, string Name, string Comment)
{
_number = Number;
_name = Name;
_comment = Comment;
}

public int Number
{
get {return _number ;}
set { _number = value;}
}

public string Name
{
get{ return _name;}
set{ _name = value;}
}

public string Comment
{
get{ return _comment; }
set{ _comment = value; }
}
}
--------------------------------------------------------------------
public class Signals<Signal>: IList<Signal>
{
private List<Signal> _list = new List<Signal>();

public Signals(SignalValidator Validator)
{
_validator = Validator;
}

public void Add(Signal item)
{
if(!_validater.Validate(item))
{
Trow new exception();
}
else
{
_list.Add(item);
}
}
}


Regards,

Bart
 
B

Bart

Sorry forgot the compiler messages:

Error 1 The best overloaded method match for
'SignalList.SignalValidator.Validate(SignalList.Signal)' has some invalid
arguments D:\Users\Bart\Visual Studio
2008\Projects\SignalList\SignalList\Signals.cs 65 13 SignalList

Error 2 Argument '1': cannot convert from 'Signal' to 'SignalList.Signal'
D:\Users\Bart\Visual Studio 2008\Projects\SignalList\SignalList\Signals.cs
65 33 SignalList

Bart
 
B

Bart

Hi Peter,

Thanks for your answers so far. I must admit that I have to chew on this.
But when you don't mind I like to come back on it.

Bart
 
B

Bart

Hi Peter,
So when you wrote "class Signals<Signal>", you aren't referencing the
existing "Signal" class. You're declaring a generic class, one that has a
type parameter named "Signal" and which other than sharing the name has
nothing to do with the actual "Signal" class.


I think you are exactly right. I didn't understand exactly what I wrote. I
thought I was referencing the Signal object. Although you made me clear that
this is not right I am still having some trouble to grasp the hole generic
thing. The reason for this is that when I write Signals.Add(new Signal(10,
"bla", bla")); I somehow think I get a reference to this Signal inside my
Signals class. But I guess that's because of my Delphi background. So I have
to do some more reading about this.
I made a guess that this is in fact what you want, thus I presented that
as my first suggestion. I apologize if the remainder of the discussion
was just confusing the issue.

No peter you didn't confuse me. Instead you opened my eyes. At the moment I
am thinking about redesigning my classes. My Signals class is a special
class that only needs to store Signals and syould be capable of validating
them. So at the moment I think that Signals only need to be composed with a
private List<Signal>. But I need to find out how to make iteratable so that
i can write foreach(Signal s in Signal){........}.

Peter I hope that I don't give you the feeling that I that this issue
doesn't have my attention because of my late responsens to your posts. But
since you are so kind to put so much effort and time in it to help me, I
feel obligated to you that I have have to tell that my mom had a hart atack
and she in the hospital right now. Luckilly she made it and she quite
allright . So that is the reason for my low response to you but as you can
understand that consumes a lot of time and energy right now.

Thanks again,

Bart
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top