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.
Start by creating a file and adding some code to it.
Visual Basic class file in the Visual Studio code editor." />
Visual Basic class file in the Visual Studio code editor." />
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.
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:
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.
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:
In this section, you'll comment out some code.
' _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
' _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
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.
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.
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.
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.
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.
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.
For Each str In qu
You see IntelliSense show you Quick Info about the query symbol.
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.
For Each str In qu
IntelliSense shows you Quick Info about the query symbol.
For Each str In query Console.WriteLine(str) Next
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 .
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 .