Tips and Tricks

Implementing Virtual Methods with Visual Assist

Visual Assist has a feature—Implement Virtual Methods—that makes it easy to implement an interface, or abstract methods of a base class. You don’t need to create anything manually.

Move the caret to a base or interface class in the declaration of your derived class, or to the derived class if you want to implement methods from all bases classes.

Open a refactoring menu—via the context menu or Shift+Alt+Q—and select Implement Virtual Methods.

blogImplVirtual

A dialog will open, letting you choose the methods to implement.

blogImplVirtualDialog

Visual Assist updates the declaration of your class, and create stubs in its implementation. By default, an exception is placed in the body of each method.

blogImplVirtualMethodCreated

You can change the default by modifying the VA Snippet for “Create from Usage Method Body”.

blogImplVirtualSnippet

If you add methods to an interface, repeat the process and Visual Assist will automatically disable existing methods.

blogImplVirtualRerun

The declaration and implementation of your new method will be inserted in the correct locations, relative to the existing methods.

blogImplVirtualDeclCreated

That’s all there is to it.

This article was contributed by Lei Zhu, student at the College of San Mateo, CA.

Leave a Reply

%d