成员'<方法>'无法通过实例引用访问

发布于 2024-12-05 04:44:09 字数 826 浏览 1 评论 0原文

整个错误文本是:

无法通过实例引用访问成员'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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独患者 2024-12-12 04:44:09

问题在于使用 Replace 方法,而不是在声明中使用 static。您需要使用类型名 Regex 而不是变量 r

DirtyString = Regex.Replace(DirtyString, "\\b" + element, "*****", RegexOptions.IgnoreCase);

原因是在 C# 中,您无法通过类型的实例访问 static 方法。这里的 Replacestatic 因此它必须通过 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 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

感受沵的脚步 2024-12-12 04:44:09

好的,首先; default(Regex) 将简单地返回 null,因为 Regex 是引用类型。因此,即使您的代码已编译,它肯定会在这一行崩溃并出现 NullReferenceException ,因为您从未向 r 分配任何有效的内容。

DirtyString = r.Replace(DirtyString, "\\b" + element, "*****", RegexOptions.IgnoreCase);

接下来,编译器会准确地告诉你问题是什么; Replace 是静态方法,而不是实例方法,因此您需要使用类型名而不是实例变量。

DirtyString = Regex.Replace(...);

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(...);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文