Environment: vs2003, vs.net
Generally, I want to evaluate the length of a string. There are two methods.
1 is the Microsoft. VisualBasic. Len function; 2 is the Length attribute in the system. string class.
The two functions are similar, but when the string is nothing (C # Is null), the first method returns 0, and the second method returns an error.
As follows:Code:
1 Dim I As Int16
2
3 Dim Stra As String
4 Stra = Nothing
5 I = Microsoft. VisualBasic. Len (stra) 'I is 0
6 I = Stra. length' throws an nullreferenceexception
7
Use reflector to check the Microsoft. VisualBasic. Len function. The original function is written as follows:
1 Public Shared Function Len () Function Len ( Byval Expression As String ) As Integer
2 If (Expression Is Nothing ) Then
3 Return 0
4 End If
5 Return Expression. Length
6 End Function
To use the Length attribute in the system. string class to implement similar functions, You can manually add segmentsProgramTo determine the null value of the string.
If Stra Is Nothing Then
I = 0
Else
I = Stra. Length
End If