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开发者_JAVA技巧;
}
The problem is with the use of the method Replace
not with the use of static in your declaration. You need to use the typename Regex
instead of the variable r
DirtyString = Regex.Replace(DirtyString, "\\b" + element, "*****", RegexOptions.IgnoreCase);
The reason why is in C# you cannot access static
methods through an instance of the type. Here Replace
is static
hence it must be used through the type Regex
Ok, so first; default(Regex)
will simply return null as Regex
is a reference type. So even if your code compiled, it would certainly crash with a NullReferenceException
at this line as you never assign anything valid to r
.
DirtyString = r.Replace(DirtyString, "\\b" + element, "*****", RegexOptions.IgnoreCase);
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.
DirtyString = Regex.Replace(...);
精彩评论