?
Solved

C#, GenericList, strange compile error

Posted on 2009-04-30
4
Medium Priority
?
655 Views
Last Modified: 2013-12-17
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
Comment
Question by:Daniel Wilson
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:RPCIT
ID: 24275457
try

System.Collections.List<Char> NameChars;


I assume it's trying to grab the wrong one from the wrong namespace
0
 
LVL 32

Author Comment

by:Daniel Wilson
ID: 24275608
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
 
LVL 6

Accepted Solution

by:
RPCIT earned 1000 total points
ID: 24281121
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
 
LVL 32

Author Comment

by:Daniel Wilson
ID: 24282365
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question