akoDwin
akoDwin

Reputation: 139

VB.NET Console program quickly get terminated

I'm pretty new in the environment of VB.NET that uses only consoles. I have a problem regarding my program, the console get quickly terminated. How can I my console from doing that? Thanks.

heres my code:

Module Module1
    Dim num1 As Integer = 0
    Dim num2 As Integer = 0
    Dim ans As Decimal = 0
    Dim choice As String


    Sub Main()
        Console.WriteLine("First Number: ")
        num1 = Console.ReadLine
        Console.WriteLine("Second Number: ")
        num2 = Console.ReadLine
        Console.WriteLine("[A] Addition")
        Console.WriteLine("[S] Subtraction")
        Console.WriteLine("[M] Multiplication")
        Console.WriteLine("[D] Division")
        Console.WriteLine("Enter your choice: ")
        choice = Console.ReadLine

        If (choice.ToUpper() = "A") Then
            ans = num1 + num2
            Console.WriteLine(ans.ToString)
        ElseIf (choice.ToUpper() = "S") Then
            ans = num1 - num2
            Console.WriteLine(ans.ToString)
        ElseIf (choice.ToUpper() = "M") Then
            ans = num1 * num2
            Console.WriteLine(ans.ToString)
        ElseIf (choice.ToUpper() = "D") Then
            ans = num1 / num2
            Console.WriteLine(ans.ToString)



        End If





    End Sub

End Module

Upvotes: 0

Views: 104

Answers (3)

Mark Hurd
Mark Hurd

Reputation: 10931

This is normally only an issue when debugging, so I add:

If Debugger.IsAttached Then _
  Console.ReadLine()

at the end of my console program Main routines.

Upvotes: 0

Jeff
Jeff

Reputation: 918

The console program has reached the end of its routine causing it to terminate. You need to put a Console.ReadLine or Console.ReadKey in the main method. This will wait for user input.

I'll usually do something like this

Sub Main()
  Console.WriteLine("Press any key to exit")
  Console.ReadKey()

  Console.WriteLine("Press enter to exit")
  Console.ReadLine()
End Sub

Upvotes: 1

faby
faby

Reputation: 7566

Console.ReadKey() should do what you are asking

put it before End Sub

refer here

Obtains the next character or function key pressed by the user. The pressed key is displayed in the console window.

Upvotes: 1

Related Questions