Domanda

Here is my code

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

When I run it it prints the A and B string twice but the C string only once. If I press enter however It does print the C sting for the second time. I know that the console.readkey is doing this.

My questions are:
Why are the strings printing twice?
Why does the console.readkey prevent the last sting from printing?
How do I make the strings print only once?

Edit: by moving the console.readkey to the end of sub main all the strings print twice

È stato utile?

Soluzione

Because you are calling this:

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

objword is pointing to the first method. You repeat this for the other methods also. The question really is why are you doing that?

Just execute this:

first(A,B,C)

The Delegate isn't necessary to simply execute a method.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top