问题
I am trying to get a VB.NET app to compile. Besides the "elephant in the room", I'm also getting 7 "'Trim' is not declared" errors on code like this:
...as well as one "'IsNothing' is not declared. It may be inaccessible due to its protection level." on this line:
If IsNothing(memberList) = False Then
I don't know VB, so there may be a simple solution to this, but I have no clue what the problems are.
回答1:
The Trim function requires a reference to Microsoft.VisualBasic from the assembly Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)
Usually is preferable to use the native Trim method from the string class and not add a reference to this assembly (mainly used to help porting old VB6 apps)
mail.CC.Add(addr.Trim())
Notice also that the string.Trim removes other whitespace characters as tabs while the Microsoft.VisualBasic function does not.
回答2:
You have to use addr.Trim
instead of Trim(addr)
Read more about Trim
in this MSDN article
And you should use
If not memberList Is Nothing Then
Instead of
If IsNothing(memberList) = False Then
Or
You have to import Microsoft.VisualBasic
namespace
回答3:
If you use the Left()
, Mid()
, and Right()
string functions you might find it easier to convert those too:
Left(t, l)
becomes t.Substring(0, l)
Mid(t, s, l)
becomes t.Substring(s-1, l)
Right(t, l)
becomes t.Substring(t.Length - l)
Often Left
and Right
are properties and stop you using the old VB string functions.
回答4:
String.Trim
doesn't take a string parameter. It returns a new string in which all leading and trailing occurrences of a set of specified characters from the current String object are removed.
It should be...
addr.Trim()
来源:https://stackoverflow.com/questions/40898683/why-am-i-getting-trim-is-not-declared-with-this-vb-net-code