Program is Visual Studio 2012 Here is an code for encrypting
Program is Visual Studio 2012. Here is an code for encrypting i\'m trying to use but i\'m having trouble with the For i = 0 to 3, since the i is already in use by an enclosing For loop. How can i fix it?
Private Sub encrypt_Click(sender As Object, e As EventArgs) Handles encrypt.Click
Dim key(3) As Integer
Dim i, j, CharASCII, KeyDigit, ComboBox As Integer
Dim CurrentChar, KeyString As String
KeyString = ComboBox
For i = 0 To RichTextBox1.Text.Length - 1
CurrentChar = RichTextBox1.Text.Substring(i, 1)
If Asc(CurrentChar) = 13 Then
RichTextBox1.Text = RichTextBox1.Text + Chr(13)
ElseIf Asc(CurrentChar) = 10 Then
RichTextBox1.Text = RichTextBox1.Text + Chr(10)
Else
KeyDigit = i Mod 4
For i = 0 To 3
key(i) = KeyString.Substring(i, 1)
Next
CharASCII = Asc(CurrentChar) + key(KeyDigit)
If CharASCII > 126 Then
CharASCII = CharASCII - 95
End If
ListBox1.Text = ListBox1.Text + Chr(CharASCII)
End If
Next
End Sub
Solution
1. In this code, Variable i is used twice.
(i) One in outer loop
(ii) Second in inner loop
2. Value of variable i has been getting modified in the inner loop. These modifications will show effect on value of i in outer loop statements.
3. Modify the inner loop using another looping variable say \'j\'
4. Inner loop has to be modified as:
For j = 0 To 3
key(j) = KeyString.Substring(j, 1)
Next
5. Now the value of i is preserved while executing inner loop and doesn\'t show any negative impact in outer loop.
