English 中文(简体)
将VB6代码转换成VB。 NET
原标题:Converting VB6 code snippet to VB.NET
  • 时间:2012-01-13 17:50:09
  •  标签:
  • vb.net
  • vb6

I am having a hard time converting this snippet to 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

It works well in VB6 but throws error when i VB.Net..

问题回答

Try this version:

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

This seems to work (just removed the "$" like Booji Boy wrote).

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
            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

仅仅为了这一目的,采取lin办法。

Function DecryptPassword(ByVal s As String) As String
    Return s.Aggregate(Of String)(String.Empty, Function(acc, c) acc & Char.ToUpper(If(Asc(c) - 5 < 124, Convert.ToChar(Asc(c) - 5), c)))
End Function

Or

Function DecryptPassword(ByVal s As String) As String
    Return New String((From c In s Select Char.ToUpper(If(Asc(c) - 5 < 124, Convert.ToChar(Asc(c) - 5), c))).ToArray)
End Function




相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签