0

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

1 Answers1

1

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.

T McKeown
  • 12,971
  • 1
  • 25
  • 32
  • because if I remove those the code won't run, or am I missing something? Edit: my mistake if I remove the variable it won't run but If i remove the method lines it works and prints the code only once. – Tim The Learner May 07 '14 at 18:21
  • you are missing something, you only need to call one of them, I don't understand why you are adding a delegate ref to the first, second and third. It doesn't look like you need to do that. – T McKeown May 07 '14 at 18:23
  • I was looking at some similar code that I wrote and expanding from that. If I remove it, It runs fine. – Tim The Learner May 07 '14 at 18:27