为什么我得到 'Trim' is not declared with this VB.NET 代码?

Why am I getting 'Trim' is not declared with this VB.NET code?

我正在尝试编译 VB.NET 应用程序。除了“”,我还在这样的代码上得到 7 个“'Trim' 未声明”错误:

...以及一个“'IsNothing' 未声明。由于其保护级别,它可能无法访问。”在这条线上:

If IsNothing(memberList) = False Then

我不知道VB,所以可能有一个简单的解决方案,但我不知道问题出在哪里。

Trim 函数需要从程序集 Visual Basic 运行时库(在 Microsoft.VisualBasic 中引用 Microsoft.VisualBasic .dll)

通常最好使用字符串 class 中的本机 Trim 方法并且不添加对此程序集的引用(主要用于帮助移植旧的 VB6 应用程序)

mail.CC.Add(addr.Trim())

另请注意,string.Trim 会删除其他空白字符作为制表符,而 Microsoft.VisualBasic 函数不会。

String.Trim 不接受字符串参数。它 returns 一个新字符串,其中删除了当前 String 对象中一组指定字符的所有前导和尾随出现。

应该是...

 addr.Trim()

您必须使用 addr.Trim 而不是 Trim(addr)

在此 MSDN article

中阅读有关 Trim 的更多信息

你应该使用

If not memberList Is Nothing Then

而不是

If IsNothing(memberList) = False Then

您必须导入 Microsoft.VisualBasic 命名空间

如果您使用 Left()Mid()Right() 字符串函数,您可能会发现转换它们也更容易:

Left(t, l) 变为 t.Substring(0, l)

Mid(t, s, l) 变为 t.Substring(s-1, l)

Right(t, l) 变为 t.Substring(t.Length - l)

通常 LeftRight 是属性,会阻止您使用旧的 VB 字符串函数。