成员'<方法>'无法通过实例引用访问方法>
整个错误文本是:
无法通过实例引用访问成员
'System.Text.RegularExpressions.Regex.Replace(string, string, string, System.Text.RegularExpressions.RegexOptions)'
;使用类型名称来限定它
这是代码。我像在另一篇文章中一样删除了“静态”,但它仍然给我错误。
我非常感谢这里所有专家的帮助 - 谢谢!
public string cleanText(string DirtyString, string Mappath)
{
ArrayList BadWordList = new ArrayList();
BadWordList = BadWordBuilder(BadWordList, Mappath);
Regex r = default(Regex);
string element = null;
string output = null;
foreach (string element_loopVariable in BadWordList)
{
element = element_loopVariable;
//r = New Regex("\b" & element)
DirtyString = r.Replace(DirtyString, "\\b" + element, "*****", RegexOptions.IgnoreCase);
}
return DirtyString;
}
The whole error text is:
Member
'System.Text.RegularExpressions.Regex.Replace(string, string, string, System.Text.RegularExpressions.RegexOptions)'
cannot be accessed with an instance reference; qualify it with a type name instead
Here's the code. I removed "static" like in another post here, but it's still giving me the error.
I'd appreciate the assistance of all the experts on here - thanks!.
public string cleanText(string DirtyString, string Mappath)
{
ArrayList BadWordList = new ArrayList();
BadWordList = BadWordBuilder(BadWordList, Mappath);
Regex r = default(Regex);
string element = null;
string output = null;
foreach (string element_loopVariable in BadWordList)
{
element = element_loopVariable;
//r = New Regex("\b" & element)
DirtyString = r.Replace(DirtyString, "\\b" + element, "*****", RegexOptions.IgnoreCase);
}
return DirtyString;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于使用
Replace
方法,而不是在声明中使用 static。您需要使用类型名Regex
而不是变量r
原因是在 C# 中,您无法通过类型的实例访问
static
方法。这里的Replace
是static
因此它必须通过Regex
类型使用The problem is with the use of the method
Replace
not with the use of static in your declaration. You need to use the typenameRegex
instead of the variabler
The reason why is in C# you cannot access
static
methods through an instance of the type. HereReplace
isstatic
hence it must be used through the typeRegex
好的,首先;
default(Regex)
将简单地返回 null,因为Regex
是引用类型。因此,即使您的代码已编译,它肯定会在这一行崩溃并出现NullReferenceException
,因为您从未向r
分配任何有效的内容。接下来,编译器会准确地告诉你问题是什么;
Replace
是静态方法,而不是实例方法,因此您需要使用类型名而不是实例变量。Ok, so first;
default(Regex)
will simply return null asRegex
is a reference type. So even if your code compiled, it would certainly crash with aNullReferenceException
at this line as you never assign anything valid tor
.Next, the compiler is telling you exactly what the problem is;
Replace
is a static method, not an instance method, so you need to use the typename as opposed to an instance variable.