Tutorial: Get started with Visual Basic in Visual Studio

  • 10 minutes to read

In this tutorial for Visual Basic (VB), you'll use Visual Studio to create and run a few different console apps and explore some features of the Visual Studio integrated development environment (IDE) while you do so.

Create a project

First, we'll create a Visual Basic application project. The project type comes with all the template files you'll need, before you've even added anything!

  1. Open Visual Studio 2017.

  2. From the top menu bar, choose File > New > Project.

  3. In the New Project dialog box in the left pane, expand Visual Basic, and then choose .NET Core. In the middle pane, choose Console App (.NET Core). Then name the project WhatIsYourName.

    Console App (.NET Core) project template in the New Project dialog box in the Visual Studio IDE

Add a workload (optional)

If you don't see the Console App (.NET Core) project template, you can get it by adding the .NET Core cross-platform development workload. You can add this workload in one of the two following ways, depending on which Visual Studio 2017 updates are installed on your machine.

Option 1: Use the New Project dialog box

  1. Click the Open Visual Studio Installer link in the left pane of the New Project dialog box.

    Click the Open Visual Studio Installer link from the New Project dialog box

  2. The Visual Studio Installer launches. Choose the .NET Core cross-platform development workload, and then choose Modify.

    .NET Core cross-platform development workload in the Visual Studio Installer

Option 2: Use the Tools menu bar

  1. Cancel out of the New Project dialog box and from the top menu bar, choose Tools > Get Tools and Features.

  2. The Visual Studio Installer launches. Choose the .NET Core cross-platform development workload, and then choose Modify.

  1. Open Visual Studio.

  2. On the start window, choose Create a new project.

    Screenshot showing the Visual Studio start window with 'Create a new project' selected.

  3. In the Create a new project window, choose Visual Basic from the Language list. Next, choose Windows from the Platform list and Console from the project types list.

    After you apply the language, platform, and project type filters, choose the Console Application template, and then choose Next.

    Screenshot showing the 'Create a new project' window with 'Visual Basic', 'Windows', and 'Console' selected in the Language, Platform, and Project Type filters and the Console Application project template selected.

    Note

    If you do not see the Console Application template, you can install it from the Create a new project window. In the Not finding what you're looking for? message, choose the Install more tools and features link.

    Screenshot showing the 'Install more tools and features' link from the 'Not finding what you're looking for' message in the 'Create new project' window.

    Then, in the Visual Studio Installer, choose the .NET Core cross-platform development workload.

    Screenshot showing the .NET Core cross-platform development workload in the Visual Studio Installer.

    After that, choose the Modify button in the Visual Studio Installer. You might be prompted to save your work; if so, do so. Next, choose Continue to install the workload. Then, return to step 2 in this "Create a project" procedure.

  4. In the Configure your new project window, type or enter WhatIsYourName in the Project name box. Then, choose Next.

    Screenshot showing the 'Configure your new project' window with the Project name field set to'WhatIsYourName'.

  5. In the Additional information window, .NET Core 3.1 should already be selected for your target framework. If not, select .NET Core 3.1. Then, choose Create.

    Screenshot showing the 'Additional information' window with .NET Core 3.1 selected in the Target Framework field.

    Visual Studio opens your new project.

  1. Open Visual Studio.

  2. On the start window, choose Create a new project.

    Screenshot showing the Visual Studio start window with 'Create a new project' selected.

  3. In the Create a new project window, choose Visual Basic from the Language list. Next, choose Windows from the Platform list and Console from the Project types list.

    After you apply the language, platform, and project type filters, choose the Console Application template, and then choose Next.

    Screenshot showing the 'Create a new project' window with 'Visual Basic', 'Windows', and 'Console' selected in the Language, Platform, and Project Type filters and the Console Application project template selected.

    Note

    If you do not see the Console Application template, you can install it from the Create a new project window. In the Not finding what you're looking for? message, choose the Install more tools and features link.

    Screenshot showing the 'Install more tools and features' link from the 'Not finding what you're looking for' message in the 'Create new project' window.

    Then, in the Visual Studio Installer, choose the .NET desktop development workload.

    Screenshot showing the .NET desktop development workload in the Visual Studio Installer.

    After that, choose the Modify button in the Visual Studio Installer. You might be prompted to save your work; if so, do so. Next, choose Continue to install the workload. Then, return to step 2 in this "Create a project" procedure.

  4. In the Configure your new project window, type or enter WhatIsYourName in the Project name box. Then, choose Next.

    Screenshot showing the 'Configure your new project' window with the Project name field set to'WhatIsYourName'.

  5. In the Additional information window, .NET 6.0 should already be selected for your target framework. If not, select .NET 6.0. Then, choose Create.

    Screenshot showing the 'Additional information' window with .NET 6.0 selected in the Framework field.

    Visual Studio opens your new project.

Create a "What Is Your Name" application

Let's create an app that prompts you for your name and then displays it along with the date and time. Here's how:

  1. If it is not already open, then open your WhatIsYourName project.

  2. Enter the following Visual Basic code immediately after the opening bracket that follows the Sub Main(args As String()) line and before the End Sub line:

                      Console.WriteLine(vbCrLf + "What is your name? ") Dim name = Console.ReadLine() Dim currentDate = DateTime.Now Console.WriteLine($"{vbCrLf}Hello, {name}, on {currentDate:d} at {currentDate:t}") Console.Write(vbCrLf + "Press any key to exit... ") Console.ReadKey(True)                                  

    This code replaces the existing WriteLine, Write, and ReadKey statements.

    Code window showing the What Is Your Name code

  3. Use the green Start button, or press F5 to build and run your first app.

  4. When the console window opens, enter your name. Your console window should look similar to the following screenshot:

    Console window showing What Is Your Name, the time and date, and Press any key to continue message

  5. Press any key to close the console window.

  1. In the WhatIsYourName project, enter the following Visual Basic code immediately after the opening bracket that follows the Sub Main(args As String()) line and before the End Sub line:

                      Console.WriteLine(vbCrLf + "What is your name? ") Dim name = Console.ReadLine() Dim currentDate = DateTime.Now Console.WriteLine($"{vbCrLf}Hello, {name}, on {currentDate:d} at {currentDate:t}!") Console.Write(vbCrLf + "Press any key to exit... ") Console.ReadKey(True)                                  

    This code replaces the existing WriteLine, Write, and ReadKey statements.

    Screenshot showing the code for the 'Program.vb' file In the 'WhatIsYourName' project loaded in the Visual Basic code editor.

  2. Use the green Start button, or press F5 to build and run your first app.

  3. When the console window opens, enter your name. Your console window should look similar to the following screenshot:

    Screenshot showing the Console window with What Is Your Name, the time and date, and Press any key to continue messages.

  4. Press any key to close the console window.

  1. In the WhatIsYourName project, enter the following Visual Basic code in the Program.vb file immediately after the opening bracket that follows the Sub Main(args As String()) line and before the End Sub line:

                      Console.WriteLine(vbCrLf + "What is your name? ") Dim name = Console.ReadLine() Dim currentDate = DateTime.Now Console.WriteLine($"{vbCrLf}Hello, {name}, on {currentDate:d} at {currentDate:t}!") Console.Write(vbCrLf + "Press any key to exit... ") Console.ReadKey(True)                                  

    This code replaces the existing WriteLine statement.

    Screenshot showing the code for the 'Program.vb' file In the 'WhatIsYourName' project loaded in the Visual Basic code editor.

  2. Use the green Start button, or press F5 to build and run your first app.

  3. When the console window opens, enter your name. Your console window should look similar to the following screenshot:

    Screenshot showing the Console window with What Is Your Name, the time and date, and Press any key to continue messages.

  4. Press any key to close the console window.

Create a "Calculate This" application

  1. Open Visual Studio 2017, and then from the top menu bar, choose File > New > Project.

  2. In the New Project dialog box in the left pane, expand Visual Basic, and then choose .NET Core. In the middle pane, choose Console App (.NET Core). Then name the file CalculateThis.

  3. Enter the following code between the Module Program line and the End Module line:

                      Public num1 As Integer Public num2 As Integer Public answer As Integer Sub Main()     Console.WriteLine("Type a number and press Enter")     num1 = Console.ReadLine()     Console.WriteLine("Type another number to add to it and press Enter")     num2 = Console.ReadLine()     answer = num1 + num2     Console.WriteLine("The answer is " & answer)     Console.ReadLine() End Sub                                  

    Your code window should look like the following screenshot:

    Code window showing the CalculateThis code

  4. Click CalculateThis to run your program. Your console window should look similar to the following screenshot:

    Console window showing the CalculateThis app, which includes prompts on which actions to take.

  1. On the start window, choose Create a new project.

  2. In the Create a new project window, choose Visual Basic from the Language list. Next, choose Windows from the Platform list and Console from the project types list.

  3. After you apply the language, platform, and project type filters, choose the Console Application template, and then choose Next.

    Then, in the Configure your new project window, type or enter CalculateThis in the Project name box. Then, choose Next.

  4. In the Additional information window, .NET Core 3.1 should already be selected for your target framework. If not, select .NET Core 3.1. Then, choose Create.

  5. Enter the following code between the Module Program line and End Module line:

                      Public num1 As Integer Public num2 As Integer Public answer As Integer Sub Main()     Console.WriteLine("Type a number and press Enter")     num1 = Console.ReadLine()     Console.WriteLine("Type another number to add to it and press Enter")     num2 = Console.ReadLine()     answer = num1 + num2     Console.WriteLine("The answer is " & answer)     Console.ReadLine() End Sub                                  

    Your code window should look like the following screenshot:

    Screenshot showing the code for the 'Program.vb' file In the 'CalculateThis' project loaded in the Visual Basic code editor.

  6. Click CalculateThis to run your program. Your console window should look similar to the following screenshot:

    Screenshot showing the Console window for the CalculateThis app, which includes prompts on which actions to take.

  1. On the start window, choose Create a new project.

  2. In the Create a new project window, choose Visual Basic from the Language list. Next, choose Windows from the Platform list and Console from the Project types list.

  3. After you apply the language, platform, and project type filters, choose the Console Application template, and then choose Next.

    Then, in the Configure your new project window, type or enter CalculateThis in the Project name box. Then, choose Next.

  4. In the Additional information window, .NET 6.0 should already be selected for your target framework. If not, select .NET 6.0. Then, choose Create.

  5. In Program.vb, enter the following code between the Module Program line and End Module line:

                      Public num1 As Integer Public num2 As Integer Public answer As Integer Sub Main()     Console.WriteLine("Type a number and press Enter")     num1 = Console.ReadLine()     Console.WriteLine("Type another number to add to it and press Enter")     num2 = Console.ReadLine()     answer = num1 + num2     Console.WriteLine("The answer is " & answer)     Console.ReadLine() End Sub                                  

    Your code window should look like the following screenshot:

    Screenshot showing the code for the 'Program.vb' file In the 'CalculateThis' project loaded in the Visual Basic code editor.

  6. Select the green Start button next to CalculateThis to run your program. Your console window should look similar to the following screenshot:

    Screenshot showing the Console window for the CalculateThis app, which includes prompts on which actions to take.

Add Git source control

Now that you've created an app, you might want to add it to a Git repository. We've got you covered. Visual Studio makes that process easy with Git tools you can use directly from the IDE.

Tip

Git is the most widely used modern version control system, so whether you're a professional developer or you're learning how to code, Git can be very useful. If you're new to Git, the https://git-scm.com/ website is a good place to start. There, you can find cheat sheets, a popular online book, and Git Basics videos.

To associate your code with Git, you start by creating a new Git repository where your code is located. Here's how:

  1. In the status bar at the bottom-right corner of Visual Studio, select Add to Source Control, and then select Git.

    Screenshot of the Git source control buttons below the Solution Explorer pane, with the Add to Source Control button highlighted.

  2. In the Create a Git repository dialog box, sign in to GitHub.

    Screenshot of the Create a Git Repository dialog window where you can sign in to GitHub.

    The repository name auto-populates based on your folder location. By default, your new repository is private, which means you're the only one who can access it.

    Tip

    Whether your repository is public or private, it's best to have a remote backup of your code stored securely on GitHub. Even if you aren't working with a team, a remote repository makes your code available to you from any computer.

  3. Select Create and Push.

    After you create your repository, you see status details in the status bar.

    Screenshot of the repo status bar that's below the Solution Explorer pane in Visual Studio.

    The first icon with the arrows shows how many outgoing/incoming commits are in your current branch. You can use this icon to pull any incoming commits or push any outgoing commits. You can also choose to view these commits first. To do so, select the icon, and then select View Outgoing/Incoming.

    The second icon with the pencil shows the number of uncommitted changes to your code. You can select this icon to view those changes in the Git Changes window.

To learn more about how to use Git with your app, see the Visual Studio version control documentation.

Quick answers FAQ

Here's a quick FAQ to highlight some key concepts.

What is Visual Basic?

Visual Basic is a type-safe programming language that's designed to be easy to learn. It is derived from BASIC, which means "Beginner's All-purpose Symbolic Instruction Code".

What is Visual Studio?

Visual Studio is an integrated development suite of productivity tools for developers. Think of it as a program you can use to create programs and applications.

What is a console app?

A console app takes input and displays output in a command-line window, also known as a console.

What is .NET Core?

.NET Core is the evolutionary next step of the .NET Framework. Where the .NET Framework allowed you to share code across programming languages, .NET Core adds the ability to share code across platforms. Even better, it's open source. (Both the .NET Framework and .NET Core include libraries of prebuilt functionality and a common language runtime (CLR), which acts as a virtual machine in which to run your code.)

Next steps

Congratulations on completing this tutorial! To learn even more, see the following tutorial.

See also

  • Visual Basic language walkthroughs
  • Visual Basic language reference
  • IntelliSense for Visual Basic code files