0

I'm having a bit of trouble with calling procedures in my program. Basically, it is a program designed to encode a user inputted message based on a key input and it works perfectly fine when I do not define any functions or procedures and everything is just under the "main" procedure.

However, now that I have defined functions and procedures I am having some trouble calling the procedures from within the "main" procedure. Below is my code:

Dim alphabet As String = "abcdefghijklmnopqrstuvwxyz "
Dim cipherText As String = "abcdefghijklmnopqrstuvwxyz "
Dim charPos As Integer = 0
Dim Cipher As String = ""
Dim Decoded As String = ""

Function userMessage()
    'User inputs message
    Dim message As String
    Console.WriteLine("Please input your message")
    message = Console.ReadLine()
    Return message
End Function

Function userKey()
    'User inputs key
    Dim key As Integer
    Console.WriteLine("Please enter your key")
    key = Console.ReadLine()
    Return key
End Function

Sub encodeMessage(ByRef message, ByRef key)
    cipherText = cipherText.Substring(key) & cipherText.Substring(0, key) 'Reorder based on key
    'Encode message
    For i = 0 To message.Length - 1
        charPos = alphabet.IndexOf(message(i))
        Cipher = Cipher & cipherText(charPos)
    Next i
    Console.WriteLine(Cipher)
End Sub

Sub decodeMessage(ByRef message, ByRef key)
    cipherText = cipherText.Substring(key) & cipherText.Substring(0, key) 'Reorder based on key
    'Decode message
    For i = 0 To Cipher.Length - 1
        charPos = cipherText.IndexOf(Cipher(i))
        Decoded = Decoded & alphabet(charPos)
    Next i
    Console.WriteLine(Decoded)
End Sub

Sub Main()
    userMessage()
    userKey()
    encodeMessage()
    decodeMessage()
    Console.ReadLine()

End Sub
End Module

The problem I find is in the main procedure where i try and call the two procedures encodeMessage and decodeMessage it is telling me

Argument not specified for parameter message of Public Sub encodeMessage

and it also shows the same error for the key.

I have tried putting it in there for example encodeMessage(key, message) and I get an error that says

key and message is not declared.

I will then declare them in the main function such as Sub Main(ByRef key, ByRef message) however, I then get a different error saying

No accessible 'Main' method with an appropriate signature was found

This is extremely frustrating and I would very much appreciate some help with this (p.s, I am very new to visual basic).

Thanks,

Thomas G
  • 9,886
  • 7
  • 28
  • 41
  • Start by turning on option strict. Then capture the return from the functions in main so you can pass them: `Dim key = userKey()` . This new local key is one of the params you defined for `encodeMessage` but are not passing – Ňɏssa Pøngjǣrdenlarp Mar 20 '16 at 20:31
  • Could you elaborate a bit for me please? i'm still confused. I changed the procedures "encodeMessage" and "decodeMessage" to functions and returned the encoded and decoded messages but I don't see any change? I still get the "No accessible "Main" method" error –  Mar 20 '16 at 20:32
  • dont change the signature for main; but collect those variables *in* main – Ňɏssa Pøngjǣrdenlarp Mar 20 '16 at 20:34
  • Ahhh I think I see what your saying now, been experimenting with it the last few minutes and it seems to be making a difference now, just attempting to get it to give the correct outputs. Thanks a lot for the help :) –  Mar 20 '16 at 20:43
  • Hey I've got one more question if you wouldn't mind lending a hand? I'm trying to error check my user inputs so that they will only accept valid inputs and so that the application will not break for example, with the "message" input. I'm aware of the "try" and "catch" functions but I can't figure out how to get it to loop the input until the user inputs for example a string as oppose to an integer? Could you possibly give me an example piece of code as to how this would work? –  Mar 20 '16 at 21:32
  • Try/Catch is not for validation or flow control. See http://stackoverflow.com/a/36117592/1070452 from just 3 hours ago – Ňɏssa Pøngjǣrdenlarp Mar 20 '16 at 21:37
  • Not sure why this question was down-voted. It seems like a beginner question, but it has code and clearly describes the problem and what was tried to solve the problem. – Chris Dunaway Mar 21 '16 at 13:50

1 Answers1

0

I just added message and key variables as global, took off the ByRef message, ByRef key parameters from encode and decode functions

That should make it work

Enjoy!

Dim alphabet As String = "abcdefghijklmnopqrstuvwxyz "
Dim cipherText As String = "abcdefghijklmnopqrstuvwxyz "
Dim charPos As Integer = 0
Dim Cipher As String = ""
Dim Decoded As String = ""
Dim message As String = ""
Dim key As Integer= ""

Function userMessage()
    'User inputs message
    Console.WriteLine("Please input your message")
    message = Console.ReadLine()
End Function

Function userKey()
    'User inputs key
    Console.WriteLine("Please enter your key")
    key = Console.ReadLine()
End Function

Sub encodeMessage()
    cipherText = cipherText.Substring(key) & cipherText.Substring(0, key) 'Reorder based on key
    'Encode message
    For i = 0 To message.Length - 1
        charPos = alphabet.IndexOf(message(i))
        Cipher = Cipher & cipherText(charPos)
    Next i
    Console.WriteLine(Cipher)
End Sub

Sub decodeMessage()
    cipherText = cipherText.Substring(key) & cipherText.Substring(0, key) 'Reorder based on key
    'Decode message
    For i = 0 To Cipher.Length - 1
        charPos = cipherText.IndexOf(Cipher(i))
        Decoded = Decoded & alphabet(charPos)
    Next i
    Console.WriteLine(Decoded)
End Sub

Sub Main()
    userMessage()
    userKey()
    encodeMessage()
    decodeMessage()
    Console.ReadLine()

End Sub
End Module
Ricardo González
  • 1,385
  • 10
  • 19