当我在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;
Run Code Online (Sandbox Code Playgroud)
...我收到此错误:
无法使用实例引用访问成员'object.Equals(object,object)'; 用类型名称来限定它.
该错误表示什么?
jav*_*iry 15
您使用的参数类型错误.您可以将其Equals用作实例级别方法或类型级别(静态)方法:
string.Equals(str1, str2, StringComparison comp);
str1.Equals(str2, StringComparison comp);
Run Code Online (Sandbox Code Playgroud)
所以,在两者中,你需要StringComparison,而不是StringComparer.你的一个:
totalValue += Session["IsChauffeurUsed"].ToString().Equals("Yes", StringComparison.CurrentCultureIgnoreCase) ? 80 : 0;
Run Code Online (Sandbox Code Playgroud)
oha*_*nho 10
“Equals”第二个参数的参数类型错误,因此编译器识别出错误的重载。
要修复它,请更改此:
StringComparer.CurrentCultureIgnoreCase
Run Code Online (Sandbox Code Playgroud)
对此:
StringComparison.CurrentCultureIgnoreCase
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7781 次 |
| 最近记录: |