Jump to content

Guide to Game Development/The Programming Language/VB.NET/Basic console input and outputs

100% developed
From Wikibooks, open books for an open world
Guide to Game Development/The Programming Language/VB.NET
How to install and create the project Basic console input and outputs Variables

If you are still at the main screen of visual studio, then you'll want to follow these steps to create the project.

Where to write your code

[edit | edit source]

You project should look like this:

Module Module1

    Sub Main()

    End Sub

End Module

You want to type all of your programming code in between

Sub Main()

and

End Sub

. You don't need to know what these do yet, just know to program between them.

Getting the console to stay open

[edit | edit source]

If you try and run the code by either pressing F5 or clicking the Green Arrow with the word "Start" you'll see that the console will open and close instantly. To fix this you'll need to input:

Console.ReadLine()

You should put this at the end of all projects that you work on until told otherwise. Once you press the Enter key while it's running, this will close.

This function has another use that we'll discuss later.

Commenting code

[edit | edit source]

You can write comments in code to help you write notes that won't be compiled. You can do this with an apostrophe (''). You can either write it on a blank line, or at the end of a line of code.

Here's an example:

'This is a comment
Console.ReadLine() 'This is also a comment

Outputting a message to the console

[edit | edit source]

To output a message to the console you'll need to use the following:

Console.WriteLine("Hello World!")

In programming languages, text (known as a string) is entered by putting speech marks around it.

All functions need parentheses () after their name like Console.ReadLine() and Console.WriteLine(), but some functions like Console.WriteLine() can have extra information entered into the function, this goes in-between the parentheses. As it's text, it important to use speech marks.

If you were to write the following:

Console.WriteLine("Hello")
Console.WriteLine("World!")

Then the output would be:

Hello
World!

It would split it up onto two lines. This is because the Console.Writeline() outputs your message, followed by the new line character, this means that any other message will go on the next line. If you don't want to add the new line character, then you can use:

Console.Write("A message")

This would mean that:

Console.Write("Hello")
Console.Write("World!")

would output:

Hello World!

Basic variable knowledge

[edit | edit source]

In order to store a user input, we'll need to learn about variables. To make a variable called Var1 which stores a string, write the following:->

Dim Var1 as String

We'll go more into variables in the next page.

Storing the input into a variable

[edit | edit source]

This is where the 'second use' of the Console.ReadLine(). This gets the user input as well as not shutting the console application. The reason that it doesn't shut the console application is because it's waiting for an input.

To store the user input in the variable, write:

Var1 = Console.ReadLine()

If you run this application you won't see much change, this is because there is no additional output so you don't notice it. You'll need to use Console.WriteLine() in order to output the variable, when outputting the variable, you shouldn't use speech marks as it's a variable name, not a string (text).

As a whole your program should now look like:

Module Module1

    Sub Main()
        'Making a variable called Var1 of type String
        Dim Var1 as String
        'Storing the input into the variable
        Var1 = Console.ReadLine()
        'Outputting the contents of the variable: Var1
        Console.WriteLine(Var1)

        'Stoping the console from closing to allow the user to read the output
        Console.ReadLine()
    End Sub

End Module

A input of:

Hello

Will output:

Hello