-2

OK so here's my code so far.

Module Module1
Public Event count()
Sub Main()
    AddHandler count, AddressOf MyFunction
    Console.WriteLine("to start the countdown process type go")
    Console.ReadLine()
    If e.KeyCode = Keys.Enter Then
        RaiseEvent count()
    End If
    Console.ReadKey()
End Sub
Sub MyFunction()
    Dim a As Integer = 16
    Dim b As Integer = 6
    Console.WriteLine("Now we are going to count to a number using only even numbers")
    Do Until b > a
        Console.WriteLine(b)
        b += 2
    Loop
    Console.ReadKey()
End Sub
End Module

I'm trying to raise the event count when the enter key is pressed. What am I doing wrong?

  • Where is `e` in Main defined? – Bradley Uffner May 12 '14 at 18:22
  • I only borrowed a snippet from someone else's code. What would I dim e As? – Tim The Learner May 12 '14 at 18:25
  • You are using code that can only work in a GUI app into a console mode app. There's little point to it, of course the Enter key was pressed after you called Console.ReadLine(). That's the only way to make your code progress. You'll also get a support call from your user, he can't find the "go" key. – Hans Passant May 12 '14 at 18:32

2 Answers2

1

Try this

Sub Main()
    AddHandler count, AddressOf MyFunction
    Console.WriteLine("to start the countdown process type go")
    Dim input As String = Console.ReadLine
    If input.ToLower = "go" Then
        RaiseEvent Count()
    Else
        Console.WriteLine("you didn't type 'go'")
        Console.ReadLine()
    End If
End Sub

To specifically answer your question about what you are doing wrong. You are mixing up two very different methods for working with user input. e is usually used inside an event handler and contains information about the event. You are working in a console application though, which doesn't raise event for input, you have to specifically poll for input. This is what console.readLine does. It returns a string that contains what the user typed. It only returns after the user pressed enter, otherwise it waits for more character. You need to get the string that the user typed and compare it to the string you are looking for. I used ToLower to force the string to all lowercase letters in so it would match no matter how the user typed it.

Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
  • Ok that works but how would it be done if I wanted a key, enter for example, to raise the event? – Tim The Learner May 12 '14 at 18:32
  • Console.ReadKey will wait for a single key press and give you the key code. But it won't give you the entire string that the user typed. Your program specifically asks the user to type 'go', without reading the entire line you have no way to see what they actually typed. Using ReadKey will wait for an single key press, like the letter 'g' in 'go'. – Bradley Uffner May 12 '14 at 18:35
1
Module Module1

    Public Event count()
    Sub Main()
        AddHandler count, AddressOf MyFunction
        Console.WriteLine("to start the countdown process type go")
        Dim input As String = Console.ReadLine() 'Already waits for 'enter'
        RaiseEvent count()


        Console.WriteLine("Press any key to exit...")
        Console.ReadKey()
    End Sub
    Sub MyFunction()
        Dim a As Integer = 16
        Dim b As Integer = 6
        Console.WriteLine("Now we are going to count to a number using only even numbers")
        Do Until b > a
            Console.WriteLine(b)
            b += 2
        Loop
        Console.ReadKey()
    End Sub

End Module
Kaizen Programmer
  • 3,798
  • 1
  • 14
  • 30