-1

in my programming class I have to create a program which allows the user in order to enter a sentence, with the buttons "Encode" and "Decode" as options. So, for "Encode", you have to translate the sentence into Asc numbers (already did this). However, I'm currently stuck on the "Decode" section, for you have to use a For loop and an array to separate the Asc numbers by spaces, then translate them into characters one by one. Here's what I have so far:

Public Class Form1
    Dim Message As String
    Dim NewMessage As String
    Dim Part As String
    Dim Part2 As Integer
    Dim Letter As String
    Dim Length As String
    Dim ints() As Integer
    Dim intlength As Integer

    Private Sub btn_Enter_Click(sender As Object, e As EventArgs) Handles btn_Enter.Click
        Message = txt_Message.Text
        Length = Message.Length() - 1

        If rbn_Encode.Checked = True Then
            For Num As Integer = 0 To Length
                Letter = Message(Num)
                Me.lbl_Code.Text = lbl_Code.Text & Asc(Letter) & " "
            Next
        End If

        If rbn_Decode.Checked = True Then
            For Num As Integer = 0 To intlength Step 1
                If Message(Num) <> " " Then
                    Part = Part & Message(Num)
                Else
                    NewMessage = NewMessage & ChrW(Part) & " "
                End If
            Next
            Me.lbl_Code.Text = NewMessage
        End If
    End Sub

    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
        Application.Exit()
    End Sub
End Class

I've been stuck on this for about 2 week, and I'm still clueless. Thank you for your help and have a wonderful day.

Douglas Barbin
  • 3,595
  • 2
  • 14
  • 34
  • 1
    not VBA , VB.NET – S Meaden May 15 '18 at 17:59
  • You should be consistent between ChrW and AscW vs Chr and Asc. But since Chr and Asc are not consistent over time, users and computers and probably don't do what you think they do, use ChrW and AscW. – Tom Blodget May 16 '18 at 16:58

1 Answers1

0

This might seem to veer off the topic of the question, but it all meanders towards a better answer.

OK there are a few issues with your code. Firstly, make sure that "Option Strict" is on - have a look here. This will force you to convert types properly and reduce the potential for problems. Bearing the above in mind,

Dim Length As String

should be

Dim Length As Integer

on to the next bit

Each procedure should have a single responsibility. Your btn_Enter.Click event handler includes code for encoding text and decoding numbers. These should be separated out into their own procedures. In a relatively short bit of code like yours, it's not too much of a problem, but even here, it makes things a little fuzzier. Have a look at the code below. There are more issues, but we'll look at them in a moment. The code below is a bit clearer and more maintainable.

Private Sub btn_Enter_Click(sender As Object, e As EventArgs) Handles btn_Enter.Click
    Message = txt_Message.Text
    Length = Message.Length() - 1

    If rbn_Encode.Checked = True Then
        EncodeTextToAscii()
    End If

    If rbn_Decode.Checked = True Then
        DecodeToText()
    End If
End Sub

Private Sub DecodeToText()
    For Num As Integer = 0 To intlength Step 1
        If Message(Num) <> " " Then
            Part = Part & Message(Num)
        Else
            NewMessage = NewMessage & ChrW(Part) & " "
        End If
    Next
    Me.lbl_Code.Text = NewMessage
End Sub

Private Sub EncodeTextToAscii()
    For Num As Integer = 0 To Length
        Letter = Message(Num)
        Me.lbl_Code.Text = lbl_Code.Text & Asc(Letter) & " "
    Next
End Sub

Next.

In your code to encode the string as ASCII, you store the resulting data directly in the label lbl_Code's text property. The user interface should never be used as the primary store for data. It's bad practice and potentially allows the user to change data accidentally - in textboxes for example. In the case of a label, it's not to important, but it's far better to get into the good habits.

To store your encoded ASCII numbers, you can use the array ints, but as your code stands, the declaration of ints is just that. There is no space in the array to store data. So, in the Encode procedure, you need to resize ints to the same as the number of characters in the string.

So now we have ..

Private Sub EncodeTextToAscii()
    ReDim ints(Length)
    For Num As Integer = 0 To Length
        Letter = Message(Num)
        ints(Num) = Asc(Letter)
    Next
End Sub

Finally onto the meat of your question. The Decode procedure can now be written as this ..

Private Sub DecodeToText()
    NewMessage = ""
    For Each asciiNumber As Integer In ints
        NewMessage = NewMessage & ChrW(asciiNumber) & " "
    Next
    Me.lbl_Code.Text = NewMessage
End Sub

Instead of mucking around getting the length of a loop and getting the ascii number in each element of an array, you can simplyfy it using a For Each statement. You dont need to know the length of the array. It just loops over the whole length. Much easier.

As an excercise, try applying the For Each idea to the Encode procedure. :-)

David Wilson
  • 4,369
  • 3
  • 18
  • 31