| title | Get started coding with VB | |
|---|---|---|
| description | Explore the code editor in Visual Studio and learn some ways that Visual Studio makes writing, navigating, and understanding Visual Basic code easier. | |
| ms.custom | vs-acquisition | |
| ms.date | 02/19/2026 | |
| ms.subservice | general-ide | |
| ms.topic | tutorial | |
| author | MikeJo5000 | |
| ms.author | mikejo | |
| dev_langs |
|
In this tutorial, you try out the code editor in Visual Studio. You 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 need to install Visual Studio, for a free version, see Visual Studio downloads.
Start by creating a file and adding some code to it.
-
Open Visual Studio. Press Esc or select Continue without code on the start window to open the development environment.
-
On the File menu on the menu bar, select New > File.
-
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 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.
:::image type="content" source="media/vs-2022/tutorial-editor.png" alt-text="Screenshot of a new Visual Basic class file in the code editor of Visual Studio.":::
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++. Add the Visual Basic Sub snippet to the file.
-
Place your cursor above the line that says
End Class, and type sub.A pop-up window appears with information about the
Subkeyword and how to insert the Sub code snippet.:::image type="content" source="media/vs-2022/tutorial-intellisense-snippet.png" alt-text="Screenshot of the IntelliSense for a Sub code snippet in Visual Studio.":::
-
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:
:::image type="content" source="media/vs-2022/tutorial-code-snippet-list.png" alt-text="Screenshot showing the Insert Snippet window with a list of category folders that contain Visual Basic code snippets.":::
In this section, you comment out some code.
:::image type="content" source="media/vs-2022/tutorial-editor-toolbar.png" alt-text="Screenshot of the toolbar in Visual Studio, which includes buttons for adding and removing code comments.":::
-
Paste the following code into the
MySub()procedure body.' _words is a string array that we'll sort alphabetically Dim _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
-
Say you're not using the
morewordsarray, 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 ofmorewordsto 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.:::image type="content" source="media/vs-2022/tutorial-comment-out.png" alt-text="Screenshot of the toolbar with the button for commenting out code highlighted in red.":::
The Visual Basic comment character
'is added to the beginning of each selected line to comment out the code.
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.
:::image type="content" source="media/vs-2022/tutorial-collapse.png" alt-text="Screenshot of the Visual Studio Code editor with the button for collapsing the outline of a section of code highlighted in red.":::
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. It's especially useful when you're collapsing long methods or entire classes.
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, right-click and select 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:
-
Right-click the word
Stringand select Peek Definition. Or press Alt+F12.A pop-up window that contains the definition of the
Stringclass appears. You can scroll within the pop-up window, or even peek at the definition of another type from the peeked code.:::image type="content" source="media/vs-2022/tutorial-peek-definition.png" alt-text="Screenshot of a Peek Definition pop-up window that contains the definition of the String class." lightbox="media/vs-2022/tutorial-peek-definition.png":::
-
Close the Peek Definition window by selecting the close button at the upper-right corner of the pop-up window.
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. 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.
-
Below the
queryvariable, start typing the following code:For Each str In qu
IntelliSense shows you Quick Info about the
querysymbol.:::image type="content" source="media/vs-2022/tutorial-intellisense-completion-list.png" alt-text="Screenshot of the IntelliSense word completion window for the word 'query' in the code editor in Visual Studio.":::
-
To insert the rest of the word
queryby using the IntelliSense word completion functionality, select Tab. -
Finish off the code block to look like the following code.
For Each str In query Console.WriteLine(str) Next
Nobody gets code right the first time. One of the things you might have to change is the name of a variable or method. Try the Visual Studio refactor functionality to rename the _words variable to words.
-
Right-click the definition of the
_wordsvariable and select Rename.A rename window appears.
-
With the variable
_wordsstill selected, enter the desired name: words. Notice that the reference towordsin the query is automatically renamed. Before you select Enter, select the Include comments checkbox in the Rename window.:::image type="content" source="media/vs-2022/tutorial-rename.png" alt-text="Screenshot of the Rename window showing the 'Include comments' checkbox as selected.":::
-
Select Enter.
Both occurrences of
wordsare renamed, in addition to the reference towordsin the code comment.
- Code snippets
- Navigate code
- Outlining
- Go To Definition and Peek Definition
- Refactor code
- IntelliSense
[!div class="nextstepaction"] Learn about projects and solutions