• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 661
  • Last Modified:

C#, GenericList, strange compile error

I am getting:
The best overloaded method match for 'System.Collections.Generic.List<char>.FindIndex(System.Predicate<char>)' has some invalid arguments      

Argument '1': cannot convert from 'char' to 'System.Predicate<char>'      

What am I doing wrong?

Thanks!
List<Char> NameChars;
//put some chars in the List
 
char[] NewName = sCurName.ToCharArray();
 
//some logic ...
NewName[i] = NameChars[NameChars.FindIndex(NewName[i]) + 1]; //Error on this line!

Open in new window

0
Daniel Wilson
Asked:
Daniel Wilson
  • 2
  • 2
1 Solution
 
RPCITCommented:
try

System.Collections.List<Char> NameChars;


I assume it's trying to grab the wrong one from the wrong namespace
0
 
Daniel WilsonAuthor Commented:
You mean System.Collections.Generic.List<Char> NameChars; -- right?

no change.  Without Generic in there, it says there's no List in System.Collections.
0
 
RPCITCommented:
interesting...  i did some research..  and found this link..

http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx

a predicate looks like it's actually a method you use to do the checking.. seems like this could be very powerful.  When I've need this functionality.. I've always just looped throught the collection and done the checking myself.
using System;
using System.Collections.Generic;
 
public class Example
{
    public static void Main()
    {
        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;
        }
    }
}

Open in new window

0
 
Daniel WilsonAuthor Commented:
Ooh, very cool!

I think in my case I should loop through ... or ... sort them once and do a quick-search.

So ... Find wanted a function that took a string, not a string -- or in my case Char.

Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now