For a course-work task at school, I have to code a simple Caesar cipher where the user can choose their own shift (+1, +2, etc.). For example, if the user selects +3 (a-->d etc.) and types in a message that contains letters x,y, or z, these letters must wrap around to the start of the alphabet so x becomes a, y becomes b, z becomes c. I am using the ASCII values to do this.
Another less important issue is that I have used quite a lot of if-else statements for each radio button to alter the Caesar shift. If possible, is there anyway to tidy this up and make it more efficient. (I have just included three if statements for each encrypt and decrypt button for examples.)
I have included my current code below.
Public Class
Dim FnlValue As String = ""
Dim FnlChar As Char
Dim VariableChr As Single
Dim caesar As Integer
Private Sub encrypt_btn_Click(sender As Object, e As EventArgs) Handles encrypt_btn.Click
FnlValue = ""
For VariableChrNo = 0 To (input.Text.Length - 1)
VariableChr = Asc(input.Text.Chars(VariableChrNo))
FnlChar = Chr(VariableChr + caesar)
FnlValue = FnlValue + FnlChar
Next
output.Text = FnlValue
If rad_2.Checked Then
caesar = 2
Else If
If rad_3.Checked Then
caesar = 3
Else If
If rad_4.Checked Then
caesar = 4
Else If
End Sub
Private Sub decrypt_btn_Click(sender As Object, e As EventArgs) Handles decrypt_btn.Click
FnlValue = ""
For VariableChrNo = 0 To (output.Text.Length - 1)
VariableChr = Asc(output.Text.Chars(VariableChrNo))
FnlChar = Chr(VariableChr - caesar)
FnlValue = FnlValue + FnlChar
Next
input.Text = FnlValue
End Sub
If rad_2.Checked Then
caesar = -2
Else If
If rad_3.Checked Then
caesar = -3
Else If
If rad_4.Checked Then
caesar = -4
Else If
End Class
See Question&Answers more detail:os