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
ReDimSyntax: Your original VB6ReDim 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 useStringBuilderinstead for in-place edits. - Data Type Mismatch: VB6's
Longis a 32-bit integer, while VB.NET'sLongis 64-bit. We'll switch toIntegerin VB.NET to match the original behavior. - Error Handling: While VB.NET supports
On Errorsyntax, it's better practice to useTry/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
- Array Type: Swapped
LongtoIntegerto align VB6's 32-bit integer with VB.NET's type system. UsingLongin VB.NET would create 64-bit integers, which could cause unexpected behavior with the XOR operation. ReDim: Your originalReDim 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.- String Modification: Replaced direct
Mid()assignment withStringBuilderbecause VB.NET strings can't be modified in-place. Note thatStringBuilderuses 0-based indexing, so we subtract 1 fromintCounterwhen accessing characters. - Input Validation: Fixed an extra
End Ifin the original VB6 code that would cause a compiler error in VB.NET. - Error Handling: Replaced the old
On Errorwith aTry/Catchblock for cleaner, more maintainable error handling (you can revert to the original syntax if needed, but modern VB.NET prefersTry/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




