T
tshad
I am confused as to why you need to use Predicate<t> in a findall method of
Generics.
I have the following which works fine.
*********************************************************
using System;
using System.Collections.Generic;
using System.Text;
namespace Generics
{
public class Racer
{
private string name;
public string Name { get{return name;}}
private string car;
public string Car {get {return car;}}
public Racer(string name, string car)
{
this.name = name;
this.car = car;
}
public override string ToString()
{
return name + ", " + car;
}
public bool GetCar(string theCar)
{
return this.car == theCar;
}
public bool GetName(string theName)
{
return this.name == theName;
}
}
public class FindRacer
{
private string car;
public FindRacer(string car)
{
this.car = car;
}
public bool DrivingCarPredicate(Racer racer)
{
return racer.Car == car;
}
}
class Program
{
public static void GetDino()
{
List<string> dinosaurs = new List<string>();
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Oviraptor");
dinosaurs.Add("Velociraptor");
dinosaurs.Add("Deinonychus");
dinosaurs.Add("Dilophosaurus");
dinosaurs.Add("Gallimimus");
dinosaurs.Add("Triceratops");
Console.WriteLine();
foreach (string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nTrueForAll(EndsWithSaurus): {0}",
dinosaurs.TrueForAll(EndsWithSaurus));
Console.WriteLine("\nFind(EndsWithSaurus): {0}",
dinosaurs.Find(EndsWithSaurus));
Console.WriteLine("\nFindLast(EndsWithSaurus): {0}",
dinosaurs.FindLast(EndsWithSaurus));
Console.WriteLine("\nFindAll(EndsWithSaurus):");
List<string> sublist = dinosaurs.FindAll(EndsWithSaurus);
foreach (string dinosaur in sublist)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine(
"\n{0} elements removed by RemoveAll(EndsWithSaurus).",
dinosaurs.RemoveAll(EndsWithSaurus));
Console.WriteLine("\nList now contains:");
foreach (string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nExists(EndsWithSaurus): {0}",
dinosaurs.Exists(EndsWithSaurus));
}
// Search predicate returns true if a string ends in "saurus".
private static bool EndsWithSaurus(String s)
{
if ((s.Length > 5) &&
(s.Substring(s.Length - 6).ToLower() == "saurus"))
{
return true;
}
else
{
return false;
}
}
static void Main(string[] args)
{
GetDino();
List<Racer> racers = new List<Racer>();
racers.Add(new Racer("Michael Schumacher", "Ferrari"));
racers.Add(new Racer("Juan Pablo Montoya", "McLaren-Mercedes"));
racers.Add(new Racer("Kimi Raikkonen", "McLaren-Mercedes"));
racers.Add(new Racer("Mark Webber", "Williams-BMW"));
racers.Add(new Racer("Rubens Barichello", "Ferrari"));
foreach (Racer r in racers)
{
Console.WriteLine(r);
}
FindRacer finder = new FindRacer("Ferrari");
foreach (Racer racer in racers.FindAll(new
Predicate<Racer>(finder.DrivingCarPredicate)))
{
Console.WriteLine(racer);
}
List<Racer> theList = racers.FindAll(new
Predicate<Racer>(finder.DrivingCarPredicate));
Console.Read();
}
}
}
*********************************************************
All this code works perfectly.
But for Racers, I do the FindAll method with:
List<Racer> theList = racers.FindAll(new
Predicate<Racer>(finder.DrivingCarPredicate));
In the GetDino method I use the following with no "Predicate<t>":
List<string> sublist = dinosaurs.FindAll(EndsWithSaurus);
Why is that?
Also, I tried set up 2 predicates similar to the GetDino method and put the
methods in the Racers Class (GetCar and GetName) but I can't get it to work:
List<Racer> theList = racers.FindAll(theList.GetCar("Ferrari"));
This will give me the error:
'System.Collections.Generic.List<Generics.Racer>' does not contain a
definition for 'GetCar'
but the class Racer does contain it.
Do you have to create a whole new class (as I do with FindRacer) to get this
to work?
Thanks,
Tom
Generics.
I have the following which works fine.
*********************************************************
using System;
using System.Collections.Generic;
using System.Text;
namespace Generics
{
public class Racer
{
private string name;
public string Name { get{return name;}}
private string car;
public string Car {get {return car;}}
public Racer(string name, string car)
{
this.name = name;
this.car = car;
}
public override string ToString()
{
return name + ", " + car;
}
public bool GetCar(string theCar)
{
return this.car == theCar;
}
public bool GetName(string theName)
{
return this.name == theName;
}
}
public class FindRacer
{
private string car;
public FindRacer(string car)
{
this.car = car;
}
public bool DrivingCarPredicate(Racer racer)
{
return racer.Car == car;
}
}
class Program
{
public static void GetDino()
{
List<string> dinosaurs = new List<string>();
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Oviraptor");
dinosaurs.Add("Velociraptor");
dinosaurs.Add("Deinonychus");
dinosaurs.Add("Dilophosaurus");
dinosaurs.Add("Gallimimus");
dinosaurs.Add("Triceratops");
Console.WriteLine();
foreach (string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nTrueForAll(EndsWithSaurus): {0}",
dinosaurs.TrueForAll(EndsWithSaurus));
Console.WriteLine("\nFind(EndsWithSaurus): {0}",
dinosaurs.Find(EndsWithSaurus));
Console.WriteLine("\nFindLast(EndsWithSaurus): {0}",
dinosaurs.FindLast(EndsWithSaurus));
Console.WriteLine("\nFindAll(EndsWithSaurus):");
List<string> sublist = dinosaurs.FindAll(EndsWithSaurus);
foreach (string dinosaur in sublist)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine(
"\n{0} elements removed by RemoveAll(EndsWithSaurus).",
dinosaurs.RemoveAll(EndsWithSaurus));
Console.WriteLine("\nList now contains:");
foreach (string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nExists(EndsWithSaurus): {0}",
dinosaurs.Exists(EndsWithSaurus));
}
// Search predicate returns true if a string ends in "saurus".
private static bool EndsWithSaurus(String s)
{
if ((s.Length > 5) &&
(s.Substring(s.Length - 6).ToLower() == "saurus"))
{
return true;
}
else
{
return false;
}
}
static void Main(string[] args)
{
GetDino();
List<Racer> racers = new List<Racer>();
racers.Add(new Racer("Michael Schumacher", "Ferrari"));
racers.Add(new Racer("Juan Pablo Montoya", "McLaren-Mercedes"));
racers.Add(new Racer("Kimi Raikkonen", "McLaren-Mercedes"));
racers.Add(new Racer("Mark Webber", "Williams-BMW"));
racers.Add(new Racer("Rubens Barichello", "Ferrari"));
foreach (Racer r in racers)
{
Console.WriteLine(r);
}
FindRacer finder = new FindRacer("Ferrari");
foreach (Racer racer in racers.FindAll(new
Predicate<Racer>(finder.DrivingCarPredicate)))
{
Console.WriteLine(racer);
}
List<Racer> theList = racers.FindAll(new
Predicate<Racer>(finder.DrivingCarPredicate));
Console.Read();
}
}
}
*********************************************************
All this code works perfectly.
But for Racers, I do the FindAll method with:
List<Racer> theList = racers.FindAll(new
Predicate<Racer>(finder.DrivingCarPredicate));
In the GetDino method I use the following with no "Predicate<t>":
List<string> sublist = dinosaurs.FindAll(EndsWithSaurus);
Why is that?
Also, I tried set up 2 predicates similar to the GetDino method and put the
methods in the Racers Class (GetCar and GetName) but I can't get it to work:
List<Racer> theList = racers.FindAll(theList.GetCar("Ferrari"));
This will give me the error:
'System.Collections.Generic.List<Generics.Racer>' does not contain a
definition for 'GetCar'
but the class Racer does contain it.
Do you have to create a whole new class (as I do with FindRacer) to get this
to work?
Thanks,
Tom