Answered by:
How to find objects in Generics with List.Find() method

Question
-
I tried to implement like this :
class car
{
string name;
float speed;
public car(string name, float speed)
{
this.name = name;
this.speed =speed;
}
public string Name{get {return name;}set{name=Value;}}
public float Speed{get {return speed;}set{speed=Value;}}
}
public class carcoll<T> : IEnumerable<T>
{
private List<T> arCar = new List<T>();
public void addCar(T obj)
{
arCar.Add(obj);
}
IEnumerator<T> GetEnumerator()
{
return arCar.GetEnumerator();
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return GetEnumerator();
}
class program
{
public static void Main()
{
carcoll<car> myCar = new carcoll<car>();
myCar.addCar(new car("civic", 150));
myCar.addCar(new car("swift", 120));
foreach (car c in myCar)
{
Console.WriteLine(c);
}
car myLocatedObject = myList.Find(delegate(car c) { return c.speed == 120; });
}
}
}
it's throwing error : carcoll<T>' does not implement interface member 'System.Collections.Generic.IEnumerable<T>.GetEnumerator()'. 'manju_ccc.carcoll<T>.GetEnumerator()' is either static, not public, or has the wrong return type.
later i modified to :
public class carcoll<T> : IEnumerable<T>
{
private List<T> arCar = new List<T>();
public void addCar(T obj)
{
arCar.Add(obj);
}
IEnumerator GetEnumerator()
{
return arCar.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
but at last same error , please assist me for further,can someone enlighten me, am fresher trying to build my carrier as .NET developer
Thanks in Advance
manjunath
- Moved by Andrew_Zhu Thursday, November 11, 2010 8:36 AM (From:.NET 4: Windows Workflow Foundation)
Wednesday, November 10, 2010 7:35 AM
Answers
-
You need to make your GetEnumerator() methods public. By default, methods that have no access modifiers are private.
For the record, the error message was pretty good in telling you what the problem is.
- Proposed as answer by Louis.fr Friday, November 12, 2010 1:18 AM
- Marked as answer by Martin_Xie Wednesday, November 17, 2010 2:26 AM
Thursday, November 11, 2010 9:18 PM
All replies
-
Care to cut and paste your ACTUAL code? Clearly myList is not defined in your example, so I strongly suspect that this is not the actual code you're running.
Thursday, November 11, 2010 8:16 PM -
You need to make your GetEnumerator() methods public. By default, methods that have no access modifiers are private.
For the record, the error message was pretty good in telling you what the problem is.
- Proposed as answer by Louis.fr Friday, November 12, 2010 1:18 AM
- Marked as answer by Martin_Xie Wednesday, November 17, 2010 2:26 AM
Thursday, November 11, 2010 9:18 PM