将VB6代码片段转换为VB.NET

dot*_*inc 2 vb.net vb6

我很难将这个代码段转换为VB.NET

Function DecryptPassword(ByVal s As String) As String
    Dim i As Integer
    Dim sPass As String = s.Trim()

    For i = 1 To Len(sPass)
        If Asc(Mid$(sPass, i, 1)) - 5 < 124 Then

            'this line throws "type char $ does not match declared data type char"
            Mid$(sPass, i, 1) = Chr$(Asc(Mid$(sPass, i, 1)) - 5) 

        Else
            Mid$(sPass, i, 1) = Mid$(sPass, i, 1)
        End If
    Next
    DecryptPassword = UCase(sPass)  ' Convert UserPassword to UpperCase
End Function
Run Code Online (Sandbox Code Playgroud)

它在VB6中运行良好但在VB.Net时抛出错误..

com*_*ech 5

试试这个版本:

Function DecryptPassword(ByVal s As String) As String

    If String.IsNullOrEmpty(s) Then
        Return String.Empty
    End If

    Dim sbPass As New System.Text.StringBuilder(s.Length)

    For Each oCharacter As Char In s.Trim
        If Asc(oCharacter) - 5 < 124 Then
            sbPass.Append(Convert.ToChar(Asc(oCharacter) - 5))
        Else
            sbPass.Append(oCharacter)
        End If
    Next
    Return sbPass.ToString.ToUpper
End Function
Run Code Online (Sandbox Code Playgroud)

  • +1.使用`Return`,`For Each`和`StringBuilder`. (3认同)