Get started coding with VB - Visual Studio (Windows) (2024)

  • Article

In this tutorial, you'll try out the code editor in Visual Studio. You'll add code to a file to learn some of the ways that Visual Studio makes writing, navigating, and understanding Visual Basic code easier.

This article assumes you're already familiar with Visual Basic. If you aren't, you might want to start with a tutorial like Create a simple Visual Basic (VB) console app.

To complete this tutorial, make sure you have the Visual Basic settings selected for Visual Studio. For information about selecting settings for the integrated development environment (IDE), see Select environment settings.

If you haven't already installed Visual Studio, go to the Visual Studio downloads page to install it for free.

Create a code file

Start by creating a file and adding some code to it.

  1. Open Visual Studio. Press Esc or select Continue without code on the start window to open the development environment.

  2. On the File menu on the menu bar, select New File.

  3. In the New File dialog, under the General category, select Visual Basic Class, and then select Open.

    A new file opens in the editor with the skeleton of a Visual Basic class. (You can already notice that you don't have to create a full Visual Studio project to gain some of the benefits that the code editor provides, such as syntax highlighting. All you need is a code file.)

    Get started coding with VB - Visual Studio (Windows) (1)

  1. Open Visual Studio. Press Esc or select Continue without code on the start window to open the development environment.

  2. On the File menu on the menu bar, select New File.

  3. In the New File dialog, under the General category, select Visual Basic Class, and then select Open.

    A new file opens in the editor with the skeleton of a Visual Basic class. (You can already notice that you don't have to create a full Visual Studio project to gain some of the benefits that the code editor provides, such as syntax highlighting. All you need is a code file.)

    Get started coding with VB - Visual Studio (Windows) (2)

Use code snippets

Visual Studio provides code snippets that you can use to quickly and easily generate commonly used code blocks. Code snippets are available for various programming languages, including Visual Basic, C#, and C++. You'll now add the Visual Basic Sub snippet to the file.

  1. Place your cursor above the line that says End Class, and type sub.

    A pop-up dialog box appears with information about the Sub keyword and how to insert the Sub code snippet.

    Get started coding with VB - Visual Studio (Windows) (3)

  2. Press Tab twice to insert the code snippet.

    The outline for the Sub procedure MySub() is added to the file.

The available code snippets vary for different programming languages. You can look at the available code snippets for Visual Basic by choosing Edit > IntelliSense > Insert Snippet (or press Ctrl+K, Ctrl+X). For Visual Basic, code snippets are available for the following categories:

Get started coding with VB - Visual Studio (Windows) (4)

There are snippets for determining if a file exists on the computer, writing to a text file, reading a registry value, executing a SQL query, or creating a For Each...Next statement, and many more.

  1. Place your cursor above the line that says End Class, and type sub.

    A pop-up window appears with information about the Sub keyword and how to insert the Sub code snippet.

    Get started coding with VB - Visual Studio (Windows) (5)

  2. Select Tab twice to insert the code snippet.

    The outline for the Sub procedure MySub() is added to the file.

The available code snippets vary for different programming languages. You can view the available code snippets for Visual Basic by right-clicking in the code editor and selecting Snippet > Insert Snippet (or by pressing Ctrl+K, Ctrl+X). For Visual Basic, code snippets are available for the following categories:

Get started coding with VB - Visual Studio (Windows) (6)

In this section, you'll comment out some code.

Get started coding with VB - Visual Studio (Windows) (7)

  1. Paste the following code into the MySub() procedure body.

    ' _words is a string array that we'll sort alphabeticallyDim _words = New String() {"the","quick","brown","fox","jumps"}Dim morewords = New String() {"over","the","lazy","dog"}Dim query = From word In _words Order By word.Length Select word
  2. Say you're not using the morewords array, but you might use it later, so you don't want to delete it. Instead, you can comment out those lines. Select the entire definition of morewords to the closing curly brace, and then select the Comment out the selected lines button on the toolbar. If you prefer to use the keyboard, select Ctrl+K, Ctrl+C.

    Get started coding with VB - Visual Studio (Windows) (8)

    The Visual Basic comment character ' is added to the beginning of each selected line to comment out the code.

Get started coding with VB - Visual Studio (Windows) (9)

  1. Paste the following code into the MySub() procedure body.

    ' _words is a string array that we'll sort alphabeticallyDim _words = New String() {"the","quick","brown","fox","jumps"}Dim morewords = New String() {"over","the","lazy","dog"}Dim query = From word In _words Order By word.Length Select word
  2. Say you're not using the morewords array, but you might use it later, so you don't want to delete it. Instead, you can comment out those lines. Select the entire definition of morewords to the closing curly brace, and then select the Comment out the selected lines button on the toolbar. If you prefer to use the keyboard, select Ctrl+K, Ctrl+C.

    Get started coding with VB - Visual Studio (Windows) (10)

    The Visual Basic comment character ' is added to the beginning of each selected line to comment out the code.

Collapse code blocks

You can collapse sections of code to focus just on the parts that interest you. To practice, try collapsing the _words array to one line of code. Select the small box with the minus sign inside it in the margin of the line that says Dim _words = New String() {. Or, if you're a keyboard user, place the cursor anywhere in the array definition and select Ctrl+M, Ctrl+M.

Get started coding with VB - Visual Studio (Windows) (11)

The code block collapses to just the first line, followed by an ellipsis (...). To expand the code block, select the same box, which now has a plus sign in it, or select Ctrl+M, Ctrl+M again. This feature is called outlining and is especially useful when you're collapsing long methods or entire classes.

You can collapse sections of code to focus just on the parts that interest you. To practice, try collapsing the _words array to one line of code. Select the down arrow in the margin of the line that says Dim _words = New String() {. Or, if you're a keyboard user, place the cursor anywhere in the array definition and select Ctrl+M, Ctrl+M.

Get started coding with VB - Visual Studio (Windows) (12)

The code block collapses to just the first line, followed by an ellipsis (...). The down arrow in the margin is now an arrow that points to the right. To expand the code block, select the > arrow, or press Ctrl+M, Ctrl+M again. This feature is called outlining and is especially useful when you're collapsing long methods or entire classes.

View symbol definitions

The Visual Studio editor makes it easy to inspect the definition of a type or class member. You can do that by navigating to the file that contains the definition, for example by right-clicking and selecting Go to Definition anywhere the symbol is referenced. An even quicker way that doesn't move your focus away from the file you're working in is to use Peek Definition. You'll now peek at the definition of the String type.

  1. Right-click the word String and select Peek Definition. Or press Alt+F12.

    A pop-up window containing the definition of the String class appears. You can scroll within the pop-up window, or even peek at the definition of another type from the peeked code.

  2. Close the Peek Definition window by selecting the close button at the upper-right corner of the pop-up window.

The Visual Studio editor makes it easy to inspect the definition of a type or class member. You can do that by navigating to the file that contains the definition, for example by right-clicking and selecting Go to Definition anywhere the symbol is referenced. An even quicker way that doesn't move your focus away from the file you're working in is to use Peek Definition. You'll now peek at the definition of the String type.

  1. Right-click the word String and select Peek Definition. Or press Alt+F12.

    A pop-up window containing the definition of the String class appears. You can scroll within the pop-up window, or even peek at the definition of another type from the peeked code.

  2. Close the Peek Definition window by selecting the close button at the upper-right corner of the pop-up window.

Use IntelliSense to complete words

IntelliSense is a valuable resource when you're coding. It can show you information about available members of a type, or parameter details for different overloads of a method. You can also use IntelliSense to complete a word after you type enough characters to disambiguate it. You'll now add a line of code to print the ordered strings to the console window, which is the standard place for output from a program to go.

  1. Below the query variable, start typing the following code:

    For Each str In qu

    You see IntelliSense show you Quick Info about the query symbol.

    Get started coding with VB - Visual Studio (Windows) (15)

  2. To insert the rest of the word query by using IntelliSense's word completion functionality, press Tab.

  3. Finish off the code block to look like the following code.

    For Each str In query Console.WriteLine(str)Next

IntelliSense is a valuable resource when you're coding. It can show you information about available members of a type, or parameter details for different overloads of a method. You can also use IntelliSense to complete a word after you type enough characters to disambiguate it. You'll now add a line of code to print the ordered strings to the console window, which is the standard place for output from the program to go.

  1. Below the query variable, start typing the following code:

    For Each str In qu

    IntelliSense shows you Quick Info about the query symbol.

    Get started coding with VB - Visual Studio (Windows) (16)

  2. To insert the rest of the word query by using the IntelliSense word completion functionality, select Tab.

  3. Finish off the code block to look like the following code.

    For Each str In query Console.WriteLine(str)Next

Refactor a name

Nobody gets code right the first time, and one of the things you might have to change is the name of a variable or method. You'll now try the Visual Studio refactor functionality to rename the _words variable to words.

  1. Right-click the definition of the _words variable and select Rename.

    A Rename window appears at the upper-right corner of the editor.

  2. With the variable _words still selected, enter the desired name: words. Notice that the reference to words in the query is automatically renamed. Before you press Enter or select Apply, select the Include comments checkbox in the Rename window.

    Get started coding with VB - Visual Studio (Windows) (17)

  3. Press Enter or select Apply.

    Both occurrences of words are renamed, in addition to the reference to words in the code comment.

Nobody gets code right the first time, and one of the things you might have to change is the name of a variable or method. You'll now try the Visual Studio refactor functionality to rename the _words variable to words.

  1. Right-click the definition of the _words variable and select Rename.

    A rename window appears.

  2. With the variable _words still selected, enter the desired name: words. Notice that the reference to words in the query is automatically renamed. Before you select Enter, select the Include comments checkbox in the Rename window.

    Get started coding with VB - Visual Studio (Windows) (18)

  3. Select Enter.

    Both occurrences of words are renamed, in addition to the reference to words in the code comment.

Related content

  • Code snippets
  • Navigate code
  • Outlining
  • Go To Definition and Peek Definition
  • Refactoring
  • Use IntelliSense

Next step

Learn about projects and solutions

Get started coding with VB - Visual Studio (Windows) (2024)

FAQs

Is Visual Studio code good for beginners? ›

What makes VSCode a suitable editor for beginners is its simplicity of use. The editor's interface is user-friendly and intuitive, which means you won't have to spend a lot of time configuring complex settings. Once installed, you're ready to code. Support for multiple programming languages is another advantage.

Is it worth learning Visual Basic in 2024? ›

VB6 end of life – why migrating becomes inevitable

Business leaders are facing the reality of their mission-critical applications becoming outdated and unreliable, in particular VB6 applications. The reasons are apparent – no active mainline support, customer demand, and changing business landscape.

Is VB easy to learn? ›

Visual Basic is a type-safe programming language that's designed to be easy to learn. A console app takes input and displays output in a command-line window, also known as a console.

Should I code in Visual Studio or Visual Studio Code? ›

Visual Studio might be the way to go if you prioritize Microsoft support and robust features for complex projects. On the other hand, if you seek versatility and a lightweight environment, Visual Studio Code could be your preferred choice.

Can Visual Studio do everything Visual Studio Code can do? ›

Despite their similar names, Visual Studio and Visual Studio Code are tailored to different aspects of the software development process. While both tools aim to enhance your efficiency as a developer and simplify code management, they're designed with distinct purposes and user bases in mind.

Is VS Code enough for coding? ›

Whether you're writing JavaScript, Python, C++, or any other language, VS Code offers a smooth coding experience. With tools for debugging, version control with Git, and customization options, it's designed to make coding efficient and enjoyable.

How do I run the first code in Visual Studio? ›

To start building the program, press the green Start button on the Visual Studio toolbar, or press F5 or Ctrl+F5. Using the Start button or F5 runs the program under the debugger. Visual Studio attempts to build and run the code in your project.

What is the best language for Visual Studio Code? ›

JavaScript is a first-class language in Visual Studio. You can use most or all of the standard editing aids (code snippets, IntelliSense, and so on) when you write JavaScript code in the Visual Studio IDE.

Is Microsoft killing Visual Basic? ›

Microsoft updated its programming languages strategy, confirming that Visual Basic will remain a going concern even though it's still relegated to second-rate status when compared to C# and F#.

Is VB still used? ›

While Visual Basic isn't as prominent as some languages, it's still used in legacy systems. Job opportunities exist, particularly in maintaining or migrating older systems. Platforms like Toptal, Upwork, Remoteplatz may offer opportunities for VB.NET developers, especially in niche or specialized projects.

Should I learn VB or VBA? ›

Answer depends on what the end goal is. If you are going to work as data analyst using Excel a lot, VBA excel is easy and limited in scope to start learning. But, VBA excel is not good to start learning programming. If you intend to become a programmer, VB is better to start with.

Is VBA dying language? ›

No, VB.NET is not dead. It is still a popular programming language and continues to be widely used for developing Windows desktop applications, as well as web and mobile applications using the .

Is VBA dead language? ›

NET ecosystem. Microsoft updated its VB language strategy on 6 February 2023, stating that VB is a stable language now and Microsoft will keep maintaining it.

Is VB better than Python? ›

In summary, Python is favored for its simplicity, versatility, and large community support, while Visual Basic is mainly used for Windows application development and offers tighter integration with Windows features.

Does Visual Studio work with Visual Basic? ›

Visual Studio 2022 has many features for Visual Basic. You can use the Visual Studio to create different Visual Basic applications like console, Windows Forms, and Windows Desktop apps.

Can I use VBA in Visual Studio Code? ›

You can call VBA code from Office solutions created by using Visual Studio, and you can also call code in Office solutions created by using Visual Studio from VBA. The specific technique differs depending on whether your Office solution is a VSTO Add-in or a document-level customization.

Can you code using Visual Studio Code? ›

Welcome to the world of using Visual Studio Code as an educator or student! In this article, we introduce you to various tools, extension packs, and learning paths that can help you get started with coding in Visual Studio Code.

Can I replace Visual Studio with VS Code? ›

For 90% or more developers, VS Code is a better choice for solving the never-ending debate of Visual Studio vs Visual Studio Code. VS Code is a cross-platform code editor that can easily run on macOS, Windows, and Linux.

Top Articles
What is the best hulless popcorn? - Chef's Resource
Best Hulless Popcorn
탱글다희 Fantrie
MyChart - Baptist Health
Ark Survival Jellyfish
Walgreens On 37Th And Woodlawn
Www Solomon's Words For The Wise
Vlb Aurora
Autorcm
Inexpensive Auto Body Repair Near Me
Isla Prize Draw Ticket
Violent Night Showtimes Near Amc Fashion Valley 18
Perverzija.com
Fox 31 News Denver Co
Call2Recycle Sites At The Home Depot
Devotion Showtimes Near Gtc Gateway Cinemas
Optum Primary Care - Winter Park Aloma
Haul auf deutsch: Was ist das? Übersetzung, Bedeutung, Erklärung - Bedeutung Online
Check From Po Box 1111 Charlotte Nc 28201
Jet Ski Rental Conneaut Lake Pa
Optum Primary Care - Winter Park Aloma
Lolalytics Aram
Max Tl Nails
Golfpro's BurgGolf Golfbanen - (Beter) leren golfen?
Katmoie
Kitco Silver Charts
Nsa Panama City Mwr
Eaton Chevrolet Gmc Houston Photos
Watch Mexico Vs Poland Free
Abby's Caribbean Cafe
Lady Wicked Playground
Txfbins
TikTok hiring Brand Protection Analyst Intern (Global E-Commerce-Governance and Experience-AMS-External Collaborations and Engagements-Brand)- 2025 Summer (MBA) in Seattle, WA | LinkedIn
pdfFiller. On-line PDF form Filler, Editor, Type on PDF, Fill, Print, Email, Fax and Export
Sunnyside Kaiser Pharmacy Hours
Putnam.schoology.com
Metro 72 Hour Extension 2022
0Gomovies To To
Truist Bank Near Here
My Vidant Chart
Jesus Calling: How Well Are You Listening?
Www Https Retirees Aa Com
Ups Drop Close To Me
Gina Wilson All Things Algebra Unit 3 Homework 2
Craigslist Classified Phoenix Arizona
Jermaine Patricia Watson
Jamie Kagol Married
Theft Crimes Lawyer in Exton | McKenzie Law Firm, P.C.
No Cable Schedule
U-Haul Moving & Storage At Valley Blvd
250 Points Standings
First Mess Blog
Latest Posts
Article information

Author: Kieth Sipes

Last Updated:

Views: 6010

Rating: 4.7 / 5 (47 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Kieth Sipes

Birthday: 2001-04-14

Address: Suite 492 62479 Champlin Loop, South Catrice, MS 57271

Phone: +9663362133320

Job: District Sales Analyst

Hobby: Digital arts, Dance, Ghost hunting, Worldbuilding, Kayaking, Table tennis, 3D printing

Introduction: My name is Kieth Sipes, I am a zany, rich, courageous, powerful, faithful, jolly, excited person who loves writing and wants to share my knowledge and understanding with you.