Visual Basic 2010 (Console) Guide
What Is The Console?
The console is an operating system window where users interact with the operating system or a text-based console application by entering text input through the computer keyboard, and reading text output from the computer terminal. For example, in Windows the console is called the command prompt window and accepts MS-DOS commands. The Console class provides basic support for applications that read characters from, and write characters to, the console.
Creating A Console Application
Start by launching Visual Basic Express from the Start Menu. Click on File, New Project and choose Console Application in the dialog box. Choose an appropriate name for your first program and click OK.
A window will pop up showing the following code.
Any code that we write in our program will go in the space coloured red. We can write as many lines of code as we need at that point but must not delete any of the lines of code automatically added for us.
At this stage, you should not worry too much about the meaning of the code that is automatically generated for you. It is important and should not be fiddled with. It will make more and more sense to you as you gain a deeper understanding of programming in VIsual Basic.
Your First Program
A good first step is to write some code that will output some text to the console window. Write the following two lines of code at the point indicated above.
Press F5 to compile and run your first program. You should see the following,
Press the enter key on the keyboard and the console window will disappear.
There were 2 lines of code. The first line of code outputted the string, Hello World! to the console. It also added a line break (you can see in the screenshot that the cursor is on the second line).
The second line causes the program to wait until the user has pressed the enter key. If you remove this line then the program still works but closes the console window after it has processed the last instruction. We will always need to include a request for keyboard input as our last instruction in a console application.
A Second Program
There are two statements that are used for outputting information to the console.
We used the first of these in our first program. It writes information to the screen and then adds a line break. The second statement will do the same thing but will not add a line break for you. Try the following instructions,
Run your program again. We have a similar effect to our first program but there are subtle differences. Firstly, we have no line breaks in this program. The first Write() statement outputs half of the message to the screen, the second statement does the other half. Try tapping a few keys before you press enter. You will see that the cursor is not on the second line this time.
The basic methods of the console are as follows,
|Console.Beep()||Makes a beepy noise through the speaker|
|Console.Beep(frequency, duration)||Makes a beepy noise at the frequency specified in hertz (must be a whole number from 37 - 32767) and for the length of time in milliseconds specified by the duration.|
|Console.Clear()||Clears the console window of text|
|Console.ReadKey()||Reads a single key pressed at the console|
|Console.ReadLine()||Reads in a line of input from the console|
|Console.Write()||Outputs information to the screen|
|Console.WriteLine()||Outputs information to the screen and adds a line break|
Having A Go
- Write a program that outputs a person's name to the console. The program should do nothing until the user presses the enter key. At this point the program should clear the screen, beep and output the text, That's not my name. You can do the whole song if you like!
- You may have noticed that the Console.ReadLine() method shows the text that the user types on the screen until they hit the enter key. Write a program that takes advantage of this fact to produce an original story each time the program runs.
- Using the WWW or any appropriate reference, write a program that uses the Console.Beep() statement to play a scale.