Alan Godfried
Alan Godfried

Reputation: 95

Why am I getting an error that says string does not contain a definition for 'Contains'

I have the simple method below:

        public static bool IsErrorMessage(String error)
    {
        var isErrorMessage = error.Left(40).Contains("ErrorMessage",StringComparison.CurrentCulture);
        return isErrorMessage;
    }

But I getting an error that says string does not contain a definition for 'Contains' and VS wants to use System.Linq.Enumerable.Contains instead.

Using .NET Framework 4.5, C#, VS 2010 and of course I have a using System directive.

Upvotes: 0

Views: 4452

Answers (2)

Scott Jones
Scott Jones

Reputation: 2906

Looks like it's preferring the LINQ extension method due to the two parameters, since String::Contains only has one param.

I think you need to flesh out your extension methods to discover the problem. Here is a compiling example that should get you started:

public static class StringExtensions
{
    public static string Left(this string s, int count)
    {
        // your method
        return "";
    }
    public static bool Contains(this string s, string contains, StringComparison comp)
    {
        // your method
        return true;
    }
}

public class Test
{
    public static bool IsErrorMessage(String error)
    {
        var isErrorMessage = error.Left(40).Contains("ErrorMessage", StringComparison.CurrentCulture);
        return isErrorMessage;
    }
}

Upvotes: 2

Mike Zboray
Mike Zboray

Reputation: 40828

There is no overload of String.Contains that takes two arguments. If you want to use a StringComparison, use IndexOf:

bool isErrorMessage = error.Left(40).IndexOf("ErrorMessage", StringComparison.CurrentCulture) > -1;

Upvotes: 6

Related Questions