.Net Programming - Financial independence

First lines of code in Visual Basic/C#

For the first lines of code in Visual basic, I have decided to create a simple console application like the one we created in the previous tutorials. We are going to explore what each line is doing from the code below. Double click on the Program.vb in solution explorer, then you will write the following :

First lines of code

Visual Basic

Module Program
    Sub Main()
       ' Writes into the console.
       Console.WriteLine("Hello all!")
    End Sub
End Module

C#

using System;

namespace BlogPostFirstLinesOfCode
{
    class Program
    {
        static void Main(string[] args)
        {
            // Writes into the console.
            Console.WriteLine("Hello All!");
        }
    }
}

It is really important to always know what the computer is doing with the code we write. The computer has no brain, you have one so you need to tell it exactly what to do.

Here Program.vb will be the first file the computer is going to access when we will hit Start. It will navigate line by line and enter the Main().

What is Main()? It is most of the time refered to as the entry point of the software, that is, the first thing that gets executed.

You can now try and run this thing! Hit Start in the Top toolbar. What is going on? If everything ran smoothly, you should have seen a black window appear and disappear instantly.

This is because the computer ran your code and then exited the program. We will have to tell it to stop so we can make sure that what is being displayed is really what we want – “Hello all!”.

Console.WriteLine()

As you can probably figure out, Console is a component that we are using to control the Console itself. In that case, WriteLine is telling the Console to Write a line. What is going between the parenthesis is what to write. In our case “Hello all!”.

Why do we put “” then?

We have to tell Visual Basic that what we give to WriteLine is a character string or if you prefer, a sentence containing multiple characters. “I am a sentence containing multiple characters :)”.

Visual Basic

' In the end it gives us the line : 
Console.WriteLine("Whatever you want to write!")

C#

// In the end it gives us the line : 
Console.WriteLine("Whatever you want to write!");

This is the core of the code, but what is with all the rest?

Lets just say that Module is a piece of the software for now and that a Sub is like a functionality, in that case, the first functionality the computer is going to execute.

Console Read Key

Visual Basic

' Add this line to the code.
Console.ReadKey()

C#

// Add this line to the code.
Console.ReadKey()

Note that the ReadKey method does not take a character string between the parenthesis. This is logical too because what we want to do now is to Read a Key into the Console. Whatever key the user is going to click there will be read by the Console and recorded.

Now try to Start the code again. What is going on now?

Console Read Key

That’s it! The Console is waiting for us to click on a key to continue its processing!

It was easy wasn’t it?! You should try changing the Console.ReadKey() for Console.ReadLine() and see what happens.

Full Visual Basic Code

Program.vb

Module Program
    Sub Main()
        ' Writes into the console.
        Console.WriteLine("Hello All!")

        ' You can write just about whatever you want!
        Console.WriteLine("Whatever you want to write!")

        ' Stops the execution and wait for user input of any key.
        Console.ReadKey()
    End Sub
End Module

Note: I’ve renamed the Module1 to Program in the solution explorer, that’s why it’s named, Program.

Full C# Code

Program.cs

using System;

namespace BlogPostFirstLinesOfCode
{
    class Program
    {
        static void Main(string[] args)
        {
            // Writes into the console.
            Console.WriteLine("Hello All!");

            // You can write just about whatever you want!
            Console.WriteLine("Whatever you want to write!");

            // Stops the execution and wait for user input of any key.
            Console.ReadKey();
        }
    }
}

Note: make sure you change the namespace (BlogPostFirstLinesOfCode) with the name of your project.

Found an error in the code? Please send me a message so I can fix it as soon as possible!

YouTube

Next article How to Work with Variables in Visual Basic
Previous article A Quick Look at the Interface of Visual Studio

Related posts

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Comment

Your data will be safe! Your e-mail address will not be published. Also other data will not be shared with third person. Required fields marked as *