Member 'object.Equals(object, object)' cannot be accessed with an instance reference; qualify it with a type name instead

后端 未结 7 438
粉色の甜心
粉色の甜心 2021-01-17 10:59

When I used the following code in C#...

int totalValue = 0;
int total = 0;
totalValue = int.Parse(Session[\"price\"].ToString()) * int.Parse(Session[\"day\"]         


        
相关标签:
7条回答
  • 2021-01-17 11:35

    You are using wrong parameter type. You can use Equals as an instance level method or a type level (static) method:

    string.Equals(str1, str2, StringComparison comp);
    
    str1.Equals(str2, StringComparison comp);
    

    So, in both, you need StringComparison, not StringComparer. And your one:

    totalValue += Session["IsChauffeurUsed"].ToString().Equals("Yes", StringComparison.CurrentCultureIgnoreCase) ? 80 : 0;
    
    0 讨论(0)
  • 2021-01-17 11:42

    Your code is not strong.

    Session is an object, it can be null, so if you want to use its value, please check the session first, and even the session's value is not a integer value.

    I suggest you to do like this:

    int? i = Session["s"] == null ? null : Parser.ParseInt(Session["s"].ToString());
    
    0 讨论(0)
  • 2021-01-17 11:44

    The correct working code:

    int totalValue = 0;
    int total = 0;
    totalValue = int.Parse(Session["price"].ToString()) * int.Parse(Session["day"].ToString());
    
    // This line
    totalValue += Session["IsChauffeurUsed"].ToString().Equals("Yes", StringComparison.CurrentCultureIgnoreCase) ? 80 : 0;
    

    Issue:

    You are using static property of StringComparer class. Rather use enum StringComparison.

    As String.Equals(str1,str2,StringComparison.CurrentCultureIgnoreCase); or str1.Equals(str2,StringComparison.CurrentCultureIgnoreCase);

    both takes enum StringComparison as there method argument.

    Now this raises some questions, why you were not able to identify this mistake in your ide.

    This is because, since StringComparer is an abstract class and CurrentCultureIgnoreCase is a static getter property, which returns an object of type StringComparer class.

    i.e,

    public static StringComparer CurrentCultureIgnoreCase { get; }
    

    Thus the compiler is treating your "Equals" method as the "Equals" method of Object Class

    i.e,

    public static bool Equals(object objA, object objB);
    

    For some other who are curious about the use of StringComparer class.

    So here is an example:

    static void Main()
        {
            // Use these two StringComparer instances for demonstration.
            StringComparer comparer1 = StringComparer.Ordinal;
            StringComparer comparer2 = StringComparer.OrdinalIgnoreCase;
    
            // First test the results of the Ordinal comparer.
            Console.WriteLine(comparer1.Equals("value-1", "value-1")); // True
            Console.WriteLine(comparer1.Equals("value-1", "VALUE-1")); // False
            Console.WriteLine(comparer1.Compare("a", "b"));
            Console.WriteLine(comparer1.Compare("a", "a"));
            Console.WriteLine(comparer1.Compare("b", "a"));
    
            // Test the results of the OrdinalIgnoreCase comparer.
            Console.WriteLine(comparer2.Equals("value-1", "value-1")); // True
            Console.WriteLine(comparer2.Equals("value-a", "value-b")); // False
            Console.WriteLine(comparer2.Equals("value-1", "VALUE-1")); // True
            Console.WriteLine(comparer2.Compare("a", "B"));
            Console.WriteLine(comparer2.Compare("a", "A"));
            Console.WriteLine(comparer2.Compare("b", "A"));
        }
    

    for more details follow https://www.dotnetperls.com/stringcomparer

    Happy coding.

    0 讨论(0)
  • 2021-01-17 11:55

    I know it's quite late and also may not apply to user's case directly, just adding this answer to help out those who face same issue but due to different reason.

    Equal() method of a string instance needs first argument as string type.

    So if by any chance first argument is not of string type and is of another type let's say int, you get the same error which can be misleading sometimes as it won't say first argument should be of type string directly.

    0 讨论(0)
  • 2021-01-17 11:57

    The Equals method is a Static method and you cannot access it via instance

    string isChauffeurUsed = Session["IsChauffeurUsed"].ToString();
    totalValue += string.Equals(isChauffeurUsed, "Yes", 
                         StringComparison.CurrentCultureIgnoreCase) 
                  ? 80 
                  : 0;
    
    0 讨论(0)
  • 2021-01-17 12:00
    int totalValue = 0;
    int total = 0;
    totalValue = int.Parse("2".ToString()) * int.Parse("2".ToString());
    string s = "Yes";
    totalValue += s.Equals("Yes",StringComparison.CurrentCultureIgnoreCase) ? 80 : 0;
    
    0 讨论(0)
提交回复
热议问题