locked
Need help. RRS feed

  • Question

  • Today i just moved a step ahead into the world of Visual Basic and translate my SB Caesar Cipher program  DGL168-2 to VB10  but i am stuck on an error. Can someone help me on this?

    here is the VB10 Code:

    Public Class Form1
    
        Dim plainmessage As String
        Dim offset As Integer
        Dim encodedmessage As String
        Dim character As String
        Dim charcode As Integer
        Dim offsetcharcode As Integer
        Dim decodedmessage As String
        Dim secrettext As String
        Dim comfreq As Integer
        Dim countl() As Integer
        Dim lfreq(25) As Integer
        Dim freq(25) As Double
        Dim diff(25) As Integer
        Dim min As Integer
        Dim possibleoffset As Integer
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            plainmessage = TextBox1.Text
            offset = NumericUpDown1.Value
            encodedmessage = ""
            If TextBox1.Text = "" Then
                MsgBox("Please Type Some Text First")
                Exit Sub
            End If
            For i = 1 To Len(plainmessage)
                character = Mid(plainmessage, i, 1)
                charcode = Asc(character)
                offsetcharcode = charcode + offset
                If charcode >= 97 And charcode <= 122 Then
                    If offsetcharcode > 122 Then
                        offsetcharcode = 97 + (offsetcharcode - 123)
                    End If
                ElseIf charcode >= 65 And charcode <= 90 Then
                    If offsetcharcode > 90 Then
                        offsetcharcode = 65 + (offsetcharcode - 91)
                    End If
                ElseIf charcode >= 48 And charcode <= 57 Then
                    If offsetcharcode > 57 Then
                        offsetcharcode = 48 + (offsetcharcode - 58)
                    End If
                Else
                    offsetcharcode = charcode
                End If
                encodedmessage = encodedmessage + Chr(offsetcharcode)
            Next
            TextBox1.Text = encodedmessage
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            encodedmessage = TextBox1.Text
            offset = NumericUpDown1.Value
            If TextBox1.Text = "" Then
                MsgBox("Please Type Some Text First")
                Exit Sub
            End If
            decodedmessage = ""
            For i = 1 To Len(encodedmessage)
                character = Mid(encodedmessage, i, 1)
                charcode = Asc(character)
                offsetcharcode = charcode - offset
                If charcode >= 97 And charcode <= 122 Then
                    If offsetcharcode < 97 Then
                        offsetcharcode = 122 - (96 - offsetcharcode)
                    End If
                ElseIf charcode >= 65 And charcode <= 90 Then
                    If offsetcharcode < 65 Then
                        offsetcharcode = 90 - (64 - offsetcharcode)
                    End If
                ElseIf charcode >= 48 And charcode <= 57 Then
                    If offsetcharcode < 48 Then
                        offsetcharcode = 57 - (47 - offsetcharcode)
                    End If
                Else
                    offsetcharcode = charcode
                End If
                decodedmessage = decodedmessage + Chr(offsetcharcode)
            Next
            TextBox1.Text = decodedmessage
        End Sub
    
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            End
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            lfreq(0) = 8.167
            lfreq(1) = 1.492
            lfreq(2) = 2.782
            lfreq(3) = 4.253
            lfreq(4) = 12.702
            lfreq(5) = 2.228
            lfreq(6) = 2.015
            lfreq(7) = 6.094
            lfreq(8) = 6.966
            lfreq(9) = 0.153
            lfreq(10) = 0.772
            lfreq(11) = 4.025
            lfreq(12) = 2.406
            lfreq(13) = 6.749
            lfreq(14) = 7.507
            lfreq(15) = 1.929
            lfreq(16) = 0.095
            lfreq(17) = 5.987
            lfreq(18) = 6.327
            lfreq(19) = 9.056
            lfreq(20) = 2.758
            lfreq(21) = 0.978
            lfreq(22) = 2.36
            lfreq(23) = 0.15
            lfreq(24) = 1.974
            lfreq(25) = 0.074
            secrettext = TextBox1.Text
            If TextBox1.Text = "" Then
                MsgBox("Please Type Some Text First")
                Exit Sub
            End If
            For i = 1 To Len(secrettext)
                character = Mid(secrettext, i, 1)
                charcode = Asc(character) - 96
                countl(charcode) = countl(charcode) + 1
            Next
            For nChar = 0 To 25
                freq(nChar) = (countl(nChar) / Len(secrettext)) * 100
            Next
            min = 3000
            For shift = 0 To 25
                For nChar = 0 To 25
                    comfreq = nChar + shift
                    If comfreq > 26 Then
                        comfreq = 0 + comfreq - 26
                    End If
                    diff(shift) = diff(shift) + Math.Abs(lfreq(nChar) - freq(comfreq))
                Next
                If diff(shift) < min Then
                    min = diff(shift)
                    possibleoffset = shift
                End If
                Label3.Text = possibleoffset
            Next
        End Sub
    End Class



    They say working hard is good but i say working smart is best...


    • Edited by 4mir '- Friday, March 2, 2012 8:20 AM added a picture
    Friday, March 2, 2012 8:17 AM

Answers

  • I am not familiar with VB. Today, I tried VB after a long time.

    You'd better confirm SB code first.

    I added textwindow.writeline(charunicode). It sometimes returns   - value.

    maybe, That's the reason.

    For i = 1 To Text.GetLength(cipheredTxt)
      charunicode = Text.GetCharacterCode(Text.GetSubText(cipheredTxt,i,1)) - 96
    TextWindow.WriteLine(charunicode)
      count[charunicode]=count[charunicode]+1

     
    Friday, March 2, 2012 2:18 PM
    Answerer
  • In VB you cannot get away with re-dimensioning arrays like you can with SB.  In your Form1 declarations, you have "count1() as Integer" without specifying how many elements the array has.  That is fine if you fill the array with data in the declaration (not in other code as you did).  Two possible solutions are to dim your variable for more elements than you need, or ReDIm the array every time you use it to maintain a flexible array size.

    Sunday, March 4, 2012 6:23 PM

All replies

  • I am not familiar with VB. Today, I tried VB after a long time.

    You'd better confirm SB code first.

    I added textwindow.writeline(charunicode). It sometimes returns   - value.

    maybe, That's the reason.

    For i = 1 To Text.GetLength(cipheredTxt)
      charunicode = Text.GetCharacterCode(Text.GetSubText(cipheredTxt,i,1)) - 96
    TextWindow.WriteLine(charunicode)
      count[charunicode]=count[charunicode]+1

     
    Friday, March 2, 2012 2:18 PM
    Answerer
  • In VB you cannot get away with re-dimensioning arrays like you can with SB.  In your Form1 declarations, you have "count1() as Integer" without specifying how many elements the array has.  That is fine if you fill the array with data in the declaration (not in other code as you did).  Two possible solutions are to dim your variable for more elements than you need, or ReDIm the array every time you use it to maintain a flexible array size.

    Sunday, March 4, 2012 6:23 PM