Export (0) Print
Expand All

Programming with Visual Basic Versus C#

You can create Visual Studio Tools for Office solutions by using either Visual Basic or C#. Because the Microsoft Office object model was designed to be used with Microsoft Visual Basic for Applications (VBA), Visual Basic developers can work comfortably with the objects exposed by the Microsoft Office applications. Developers who use C# face unique challenges working with the Microsoft Office object model.

Key Differences

The following table shows key differences between using C# and Visual Basic with Visual Studio Tools for Office.

Feature Description Visual Basic support C# support

Passing Parameters

The Excel primary interop assemblies accept parameters passed by value, but the Word primary interop assemblies require each parameter to be passed by reference.

Both Visual Basic and C# pass parameters by value.

No additional work is needed to pass parameters by reference.

Using C# in Word projects, optional parameters must be passed using the ref keyword, and you must pass a variable containing the actual value. You cannot pass literal values to Word methods.

Optional Parameters

Many Microsoft Office methods allow 20, 30, or more optional parameters.

Visual Basic supports optional parameters on method calls.

C# does not support optional parameters. Microsoft Visual Studio 2005 Tools for the Microsoft Office System projects include a global variable named missing that is assigned System.Type.Missing in the generated code. You can pass this global variable to assume the default value of each optional parameter. You must pass the default value explicitly for parameters passed by value.

Parameterized properties

VBA supports properties that accept parameters, acting as read-only functions.

Visual Basic supports properties that accept parameters.

C# requires accessor methods for setting and retrieving property values that accept parameters. The Excel primary interop assemblies provide accessor get/set methods for each parameterized property. The Word primary interop assemblies provide accessor get/set methods that accept Variant parameters.

Late binding

VBA can determine properties of objects at run time, effectively providing late binding between your code and the objects themselves.

Both C# and Visual Basic developers can take advantage of the System.Reflection namespace, which allows running code to determine available members of a specified type and performing a type of late binding.

Visual Basic can handle late binding by using the Option Strict Off directive.

C# does not handle late binding.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft