You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

VB6转VB.NET:EnDecrypt函数翻译求助,ReDim语句存疑

VB6 to VB.NET: EnDecrypt Function Conversion Fixes

Hey there, let's get this EnDecrypt function converted properly—you're right about the ReDim part, but there are a few other VB6 vs VB.NET behavior differences you need to watch out for (like immutable strings!) that were probably breaking your code.

Key Issues to Fix

  • ReDim Syntax: Your original VB6 ReDim intKeyChars(1 To intKeyLen) works directly in VB.NET, but we need to adjust the array's type first (more on that below).
  • String Immutability: VB6 lets you modify individual characters in a string with Mid() = ..., but VB.NET strings are immutable—this will throw an error if you try it. We'll use StringBuilder instead for in-place edits.
  • Data Type Mismatch: VB6's Long is a 32-bit integer, while VB.NET's Long is 64-bit. We'll switch to Integer in VB.NET to match the original behavior.
  • Error Handling: While VB.NET supports On Error syntax, it's better practice to use Try/Catch, but I'll include both options for you.

Converted VB.NET Code

Public Function EnDecrypt(ByVal strInputText As String, ByRef strOutputText As String) As Boolean
    ' Use Try/Catch for modern error handling (recommended)
    Try
        ' Private vars - adjusted types to match VB6 behavior
        Dim intKeyChars() As Integer ' VB6 Long = VB.NET Integer
        Dim intKeyChr As Integer
        Dim intKeyIndex As Integer
        Dim intKeyLen As Integer
        Dim intTextLen As Integer
        Dim intCounter As Integer
        Dim strInputKey As String = "TEST1290"

        intTextLen = strInputText.Length
        intKeyLen = strInputKey.Length
        intKeyIndex = intKeyLen

        ' Validate input (fixed the extra End If from original VB6 code)
        If intKeyLen = 0 OrElse intTextLen = 0 Then
            strOutputText = String.Empty
            Return False
        End If

        ' Your original ReDim works exactly the same in VB.NET!
        ReDim intKeyChars(1 To intKeyLen)

        ' Populate key array
        For intCounter = 1 To intKeyLen
            intKeyChars(intCounter) = Asc(Mid(strInputKey, intCounter, 1))
        Next intCounter

        ' Use StringBuilder to modify string characters (VB.NET strings are immutable)
        Dim inputBuilder As New System.Text.StringBuilder(strInputText)
        For intCounter = 1 To intTextLen
            ' Get next key character
            intKeyChr = intKeyChars(intKeyIndex)
            ' Encrypt/decrypt the character
            Dim originalChar As Char = inputBuilder(intCounter - 1) ' VB.NET uses 0-based indexes for StringBuilder
            Dim encryptedChar As Char = Chr(Asc(originalChar) Xor intKeyChr)
            inputBuilder(intCounter - 1) = encryptedChar
            ' Update key character (avoid overflow)
            intKeyChars(intKeyIndex) = (intKeyChr + 32) Mod 126
            ' Move to next key index
            intKeyIndex = (intKeyIndex Mod intKeyLen) + 1
        Next intCounter

        ' Return results
        strOutputText = inputBuilder.ToString()
        Return True
    Catch ex As Exception
        strOutputText = String.Empty
        Return False
    End Try

    ' If you prefer to keep the original On Error syntax (not recommended, but compatible):
    'On Error Resume Next
    'On Error GoTo ErrorHandler
    '... (rest of code with StringBuilder fix)
    'Exit Function
    'ErrorHandler:
    'strOutputText = String.Empty
    'Return False
End Function

Breakdown of Critical Changes

  1. Array Type: Swapped Long to Integer to align VB6's 32-bit integer with VB.NET's type system. Using Long in VB.NET would create 64-bit integers, which could cause unexpected behavior with the XOR operation.
  2. ReDim: Your original ReDim intKeyChars(1 To intKeyLen) is fully supported in VB.NET—no changes needed here! The confusion probably came from mixing up array initialization syntax, but this line works as-is.
  3. String Modification: Replaced direct Mid() assignment with StringBuilder because VB.NET strings can't be modified in-place. Note that StringBuilder uses 0-based indexing, so we subtract 1 from intCounter when accessing characters.
  4. Input Validation: Fixed an extra End If in the original VB6 code that would cause a compiler error in VB.NET.
  5. Error Handling: Replaced the old On Error with a Try/Catch block for cleaner, more maintainable error handling (you can revert to the original syntax if needed, but modern VB.NET prefers Try/Catch).

Testing Tip

Make sure to test with the same input as your VB6 code to verify the encryption/decryption matches—this will confirm the conversion works correctly.

内容的提问来源于stack exchange,提问作者Stefan van de Laarschot

火山引擎 最新活动