Reputation: 2163
I have a problem translating VB6 To VB.NET i need this translated for the new functionality.
I've tried to translate VB6 code to VB.net but it failed maybe someone can help me out here
I don't know what ReDim intKeyChars(1 To intKeyLen)
should look like the rest i think can be copy paste to VB.NET. VB6 behaves different from VB.NET in some cases.
Public Function EnDecrypt( _
ByVal strInputText As String, _
ByRef strOutputText As String _
) As Boolean
On Error Resume Next
On Error GoTo ErrorHandler
' Private vars
Dim intKeyChars() As Long
Dim intKeyChr As Long
Dim intKeyIndex As Long
Dim intKeyLen As Long
Dim intTextLen As Long
Dim intCounter As Long
Dim strInputKey As String
' Set input key
strInputKey = "TEST1290"
' Move key chars into an array of long to speed up code
intTextLen = Len(strInputText)
intKeyLen = Len(strInputKey)
intKeyIndex = intKeyLen
If (intKeyLen = 0) Or (intTextLen = 0) Then
GoTo ErrorHandler
End If
ReDim intKeyChars(1 To intKeyLen)
For intCounter = 1 To intKeyLen
intKeyChars(intCounter) = Asc(Mid(strInputKey, intCounter, 1))
Next intCounter
For intCounter = 1 To intTextLen
' Get the next char in the password
intKeyChr = intKeyChars(intKeyIndex)
' EnDecrypt one character in the string
Mid(strInputText, intCounter, 1) = Chr(Asc(Mid(strInputText, intCounter, 1)) Xor intKeyChr)
' Modify the character in the password (avoid overflow)
intKeyChars(intKeyIndex) = (intKeyChr + 32) Mod 126
' Prepare to use next char in the password
intKeyIndex = (intKeyIndex Mod intKeyLen) + 1
Next intCounter
' Return values
strOutputText = strInputText
EnDecrypt = True
Exit Function
ErrorHandler:
' Return values
strOutputText = vbNullString
EnDecrypt = False
End Function
Upvotes: 0
Views: 1097
Reputation: 2089
The ReDim statement can look like this:
ReDim intKeyChars(intKeyLen)
VB.NET arrays are always lower-bounded with zero, and thus ReDim only takes one parameter, the upper bound. Note that unlike C#, the upper bound index will be UBound(intKeyChars), not UBound(intKeyChars)-1, so the rest of the code should work. (intKeyChars element zero will be unused.)
Upvotes: 0
Reputation: 6131
Let's step through what the code actually does:
ReDim intKeyChars(1 To intKeyLen)
The ReDim keyword literally re-declares the intKeyChars variable where as the 1 to intKeyLen specifies that you want the bottom of the array to start at an index of 1 (which was common in legacy VB code) and the top of the array to end at an index of whatever the value of intKeyLen is.
There are some things that you'll need to adjust. Firstly, arrays cannot have an index of 1 in Visual Basic .NET, they must have an index of 0.
Secondly, the reason why legacy VB code used the ReDim statement to add items to an array was because there was no simple method of adding or removing items to a collection, you literally had to reallocate the memory and add or remove any values at that time. Luckily in Visual Basic .NET we have the List(Of T) collection that gives us built in methods such as Add, AddRange, Insert, Remove, RemoveAt, and RemoveRange. But after looking at the code some more, the original legacy code should've just declare the array with the specified upper-bounds equal to the String's length in the first place (no need to ReDim the array each time).
So in your case, the updated code may look something like this:
Dim intKeyChars(strInputKey.Length - 1) As Integer
For intCounter As Integer = 0 To intKeyChars.Length - 1
intKeyChars(intCounter) = Asc(strInputKey(intCounter))
Next
Upvotes: 3