字符串打印除了上次以外多次?

时间:2014-05-07 18:13:05

标签: vb.net string delegates console-application console.readkey

这是我的代码

Module Module1
    Private Delegate Sub word(ByVal A As String, ByVal B As String, C As String)
    Dim A, B, C As String
    Sub Main()
        Console.WriteLine("type something")
        A = Console.ReadLine()
        Console.WriteLine("type something else")
        B = Console.ReadLine()
        Console.WriteLine("type something else")
        C = Console.ReadLine()
        Dim objword As New word(AddressOf first)
        objword(A, B, C)
        first(A, B, C)
        objword = New word(AddressOf Second)
        objword(A, B, C)
        Second(A, B, C)
        objword = New word(AddressOf third)
        objword(A, B, C)
        third(A, B, C)
    End Sub
    Sub first(ByVal A As String, ByVal B As String, C As String)
        Console.WriteLine(A)
    End Sub
    Sub Second(ByVal A As String, ByVal B As String, C As String)
        Console.WriteLine(B)
    End Sub
    Sub third(ByVal A As String, ByVal B As String, C As String)
        Console.WriteLine(C)
        Console.ReadKey()
    End Sub

End Module

当我运行它时,它会打印两次A和B字符串,但只打印一次C字符串。如果我按Enter键,它会第二次打印C sting。我知道console.readkey正在这样做。

我的问题是:
 为什么字符串打印两次?
 为什么console.readkey会阻止最后一次打印呢?  如何只打印一次字符串?

编辑:通过将console.readkey移动到sub main的末尾,所有字符串都打印两次

1 个答案:

答案 0 :(得分:1)

因为你这称呼:

 objword(A, B, C)
 first(A, B, C)

objword指向first方法。您也可以为其他方法重复此操作。问题是你为什么这样做?

执行此操作:

first(A,B,C)

代表不需要简单地执行方法。