Visual Basic调用过程

时间:2016-03-20 20:11:46

标签: vb.net stored-procedures procedures

我在程序中调用程序时遇到了一些麻烦。基本上,它是一个程序,旨在根据键输入对用户输入的消息进行编码,当我没有定义任何功能或程序时,它完全正常工作,而且一切都在" main"过程

但是,现在我已经定义了函数和过程,我在调用" main"中的过程时遇到了一些麻烦。程序。以下是我的代码:

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

我发现问题出在main程序中,我试着调用两个程序encodeMessagedecodeMessage它告诉我

  

未为 Public Sub encodeMessage

的参数消息指定参数

并且它还显示key的相同错误。

我已尝试将其放在那里,例如encodeMessage(key, message),我收到错误消息

  

密钥消息未被声明。

然后我会在Sub Main(ByRef key, ByRef message)之类的主函数中声明它们,然而,我得到一个不同的错误说

  

无法访问' 主要'找到了具有适当签名的方法

这非常令人沮丧,我非常感谢这方面的帮助(p.s,我对视觉基础很新)。

谢谢,

1 个答案:

答案 0 :(得分:0)

我刚刚将消息和关键变量添加为全局,从编码和解码函数中取消ByRef消息,ByRef关键参数

那应该让它发挥作用

享受!

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