ARCHITECTURAL TOOLS

The code editor provides several powerful tools that can help you understand the structure of your code and how to navigate through its pieces. They can give you a better understanding of how the pieces of the program fit together, and they can help you track down important code snippets, such as where a variable or type is defined and where one piece of code is called by others.

The following sections describe the most useful of these kinds of architectural tools and explain how to invoke them.

Rename

If you right-click the definition or occurrence of a symbol, such as a variable, subroutine, function, or class, and select Rename, Visual Studio displays a dialog box where you can enter a new name for the item. If you enter a name and click OK, Visual Studio updates all references to that symbol. If the symbol is a variable, it changes all references to the variable so they use the new name.

This is much safer than using a simple textual find-and-replace, which can wreak havoc with strings that contain your target string. For example, if you textually replace the variable name factor with issue, your Factorial function becomes Issueial. In contrast, if you right-click the factor variable, select Rename, and set the new name to issue, Visual Studio only updates references to the variable.


CORRUPTED COMMENTS
Unfortunately, Rename still leaves any comments that discuss the factor variable unchanged. You’ll have to search the comments to fix them.

Go To Definition

If you right-click a symbol or type, such as a variable, function, or class, and select Go To Definition, the code editor jumps to the location where the symbol is defined. For example, it would jump to a variable’s declaration or a function’s definition.

If the symbol you clicked is defined by Visual Basic or a library rather than your code, Visual Studio opens the Object Browser and displays the symbol’s definition there.

Go To Type Definition

If you right-click a variable and click Go To Type Definition, the code editor jumps to the location where the symbol’s data type is defined. For example, if you right-click a variable of type Employee, the editor would jump to the definition of the Employee class.

If you click a variable that has one of the predefined data types such as Integer, Double, or String, the editor displays the Object Browser entry for that type.

Highlight References

Whenever the cursor sits on a symbol, the code editor highlights all references to that symbol by giving them a light gray background. It’s a subtle effect, so you may not even notice it unless you’re looking for it.

Reference highlighting makes it easier to see where a symbol such as a variable or subroutine is used, although it only really works locally. If a subroutine is called from many pieces of code that are far apart, you’ll see only the ones that are currently visible in the code editor’s window.

When you have a reference highlighted, you can use Ctrl+Shift+Up Arrow and Ctrl+Shift+Down Arrow to move to the next or previous reference.

To learn more about references to a symbol that are farther away, use the Find All References command described next.

Find All References

If you right-click a symbol such as a subroutine or variable and select Find All References, Visual Studio displays a list of everywhere in the program that uses that symbol.

For example, if you right-click a call to a function named Fibonacci, the list includes all calls to that function plus the function’s definition.

You can double-click any of the listed references to make the code editor quickly jump to that reference.

Generate From Usage

The code editor can provide methods for automatically generating pieces of code in the form of suggested error corrections. For example, suppose you have not defined a Person class but you type the following code:

Dim new_student As New Person()

The code editor correctly flags this as an error because the Person class doesn’t exist. It underlines the word Person with a blue squiggly line and displays a short red rectangle near it. If you hover over the rectangle, you’ll see an error icon. If you then click the icon, Visual Studio displays a list of suggested corrections that include:

  • Change Person to Version
  • Generate ‘Class Person’
  • Generate New Type

The first choice assumes you have made a simple spelling error.

The second choice creates a new empty class named Person. You can fill in its properties and methods later.

The third choice displays the dialog box shown in Figure 5-10 so you can make Person another data type that might make sense such as an Enum or Structure. The dialog box lets you set the type’s access to Default, Friend, or Public, and specify the file where Visual Studio should create the new type.

FIGURE 5-10: The Generate New Type dialog box lets you create a new Class, Enum, or Structure.

image

Now suppose you create an empty Person class and then type the following code:

new_student.FirstName = "Zaphod"

The code editor also flags this statement as an error. If you click the error icon this time, the suggested solution says:

Generate property stub for ‘FirstName’ in ‘WindowsApplication1.Person’

If you click this text, Visual Studio adds the following simple property to the Person class:

Property FirstName As String

The code editor can also generate a constructor for the class if you enter the following code:

Dim another_person As New Person("Trillian")

This code is flagged as an error because no constructor is defined that takes a parameter. The error suggestions can make a constructor for you, although you’ll need to edit it to give it code that handles the parameter.

This also causes a new error because the class now has a constructor that takes a single parameter, but not one that takes no parameters, so the earlier statement Dim new_student As New Person() is flagged as an error.

By now you can probably guess what’s coming: If you click the error icon, the suggestions can make a constructor for this case, too.

Similarly, you can use the error suggestions to generate stubs for subroutines and functions. Simply use the new items as if they already exist, use the error suggestions to build stubs, and then fill in the appropriate code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.195.215