Wednesday, January 21, 2009

VB.net2008:Creating Console Application

Another common type of application in visual basic.net application is the console application and we'll be using such application during next feww hours as we study the visual basic languauge . to create a console application you select

File>> New project>>Console Application


There,s no user interface in console application: the output of such an application simply appears directly in a DOS window as text


The Sub Main() and End Sub part here is visual Basic's way of creating a sub procedure (the detail of sub procedure we explain later) .That name is Main here ,and code with that name is automatically run when a console Application starts. We can put the code we want to execute when the console application runs in the Main Sub procedure. For example,to display the message "Hello There "--- we can use this line of code

module Module1

Sub Main()

Console.writeline("Hello There")

end Sub()


Because console application dont have textboxs or user - interface element you cant display text in such application.instead, you can use Console.writeline to write linews of text.

you can now run this new console application in usual way--select Debug,Start menu item or by pressing F5 . A DOS Window will appear with the text "Hello There" in it.But it'll close as fast as it opened,You can solve this problem by intentionally keeping the console widow open until you're ready to dismiss it ----for example until to press Enter Key. and you do that by inserting the Console.readline() code.Such lines of text are read when you press the Enter Key ,ending your input. to make this work .we can display a prompt saying "Press Enter To Continue..."
and then use Console.Readline() to wait until the Enter key is pressed.It Look like this

Module Module1

Sub Main()
Console.WriteLine("hello there")
Console.WriteLine("Press Enter To Continue...")
Console.ReadLine()
End Sub

End Module

and thats all we need! You can see the increament of new line code in below picture 26



When you run application by pressing F5 or using Debug ,Start you will see the result like the below figure27

Console Application like thes provide you an easy way of working with Visual Basic without worrying about the detail of creating and handling a user interface.All you to do is to display text to use Console.Writeline(), and to read what user has typed, you use Console.Readline

No comments:

Post a Comment