简体   繁体   English

不能使用实例引用访问成员“object.Equals(object, object)”; 用类型名称来限定它

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

When I used the following code in C#...当我在 C# 中使用以下代码时...

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

// This line causes the error
totalValue += Session["IsChauffeurUsed"].ToString().Equals("Yes", StringComparer.CurrentCultureIgnoreCase) ? 80 : 0;

... I received this error : ...我收到此错误:

Member 'object.Equals(object, object)' cannot be accessed with an instance reference;不能使用实例引用访问成员“object.Equals(object, object)”; qualify it with a type name instead.用类型名称来限定它。

What does that error indicate?该错误表示什么?

You are using wrong parameter type.您使用了错误的参数类型。 You can use Equals as an instance level method or a type level (static) method:您可以将Equals用作实例级方法或类型级(静态)方法:

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

str1.Equals(str2, StringComparison comp);

So, in both, you need StringComparison , not StringComparer .因此,在两者中,您都需要StringComparison ,而不是StringComparer And your one:还有你的:

totalValue += Session["IsChauffeurUsed"].ToString().Equals("Yes", StringComparison.CurrentCultureIgnoreCase) ? 80 : 0;

The Equals method is a Static method and you cannot access it via instance Equals 方法是静态方法,您不能通过实例访问它

string isChauffeurUsed = Session["IsChauffeurUsed"].ToString();
totalValue += string.Equals(isChauffeurUsed, "Yes", 
                     StringComparison.CurrentCultureIgnoreCase) 
              ? 80 
              : 0;

Your argument for the second parameter of 'Equals' has the wrong type, so the compiler is identifying the wrong overload.您对 'Equals' 的第二个参数的参数类型错误,因此编译器正在识别错误​​的重载。

To fix it, change this:要修复它,请更改此:

StringComparer.CurrentCultureIgnoreCase

to this:对此:

StringComparison.CurrentCultureIgnoreCase

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.您正在使用StringComparer类的静态属性。 Rather use enum StringComparison .而是使用 enum StringComparison

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

both takes enum StringComparison as there method argument.两者都将 enum StringComparison 作为方法参数。

Now this raises some questions, why you were not able to identify this mistake in your ide.现在这提出了一些问题,为什么你不能在你的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.这是因为StringComparer是一个抽象类,CurrentCultureIgnoreCase是一个静态 getter 属性,它返回一个StringComparer类类型的对象

ie, IE,

public static StringComparer CurrentCultureIgnoreCase { get; }

Thus the compiler is treating your " Equals " method as the " Equals " method of Object Class因此编译器将您的“ Equals ”方法视为Object Class的“ Equals ”方法

ie, IE,

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

For some other who are curious about the use of StringComparer class.对于其他对StringComparer类的使用感到好奇的人。

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有关更多详细信息,请访问 https://www.dotnetperls.com/stringcomparer

Happy coding.快乐编码。

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.字符串实例的Equal()方法需要第一个参数作为string类型。

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.因此,如果有任何机会第一个参数不是string类型而是另一种类型,比如说int ,您会得到相同的错误,有时可能会产生误导,因为它不会直接说第一个参数应该是字符串类型。

totalValue += string.Equals(Session["IsChauffeurUsed"].ToString(), "Yes", StringComparison.CurrentCultureIgnoreCase) ? 80 : 0;

(我无法编译它来测试它,但我认为它应该可以工作)

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. Session是一个对象,它可以为空,所以如果你想使用它的值,请先检查 session,即使 session 的值不是整数值。

I suggest you to do like this:我建议你这样做:

int? i = Session["s"] == null ? null : Parser.ParseInt(Session["s"].ToString());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用实例引用访问成员“object.Equals(object, object)” - Member 'object.Equals(object, object)' cannot be accessed with an instance reference 成员 'object.equals(object?, object?' 不能通过实例引用访问;在列表删除 c#? - Member 'object.equals(object?, object?' cannot be accessed with an instance reference; quality it with a name instead on string in list removal c#? 使用实例引用无法访问成员'string.Format(string,params object [])';用类型名称来限定它 - Member 'string.Format(string, params object[])' cannot be accessed with an instance reference; qualify it with a type name instead 成员不能通过实例引用进行访问; 用类型名称代替它 - member cannot be accessed with an instance reference; qualify it with a type name instead 无法使用实例引用访问成员; 用类型名称代替它 - Member cannot be accessed with an instance reference; qualify it with a type name instead 会员 <method> 不能使用实例引用进行访问; 用类型名称代替它 - member <method> cannot be accessed with an instance reference; qualify it with a type name instead “无法通过实例引用访问成员,请改为使用类型名称来限定它”,而我确实使用类型名称 - “Member cannot be accessed with an instance reference qualify it with a type name instead” and I DO use type name 成员&#39;LockscreenNotification.App.RootFrame.get&#39;不能通过实例引用进行访问,而应使用类型名称对其进行限定 - Member 'LockscreenNotification.App.RootFrame.get' cannot be accessed with an instance reference, qualify it with an type name instead 无法通过实例引用访问成员“Camera.main”; 改为使用类型名称来限定它 - Member 'Camera.main' cannot be accessed with an instance reference; qualify it with a Type name instead 无法使用实例引用访问成员“Storyboard.SetTargetName(DependencyObject, string)”; 用类型名称来限定它 - Member 'Storyboard.SetTargetName(DependencyObject, string)' cannot be accessed with an instance reference; qualify it with a type name instead
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM