VSTO - Parameterized Properties

VBA has a large number of properties that can accept parameters.
There is no support for properties that accept parameters in C#.
This also includes Indexed Properties
Regardless of which version of C# or Visual Studio you are using, properties that accept parameters are not supported.
If you cannot retrieve or set a property's value directly when using COM Interop, it may be because the property is expecting a parameter.
There are two ways you can get around this problem:
Visual Studio 2010 and Later - You can use Indexer Syntax.
Visual Studio 2008 and Earlier - You can use the Get and Set Acessor Methods.


Properties with Parameters

Excel.Range. The Range property can accept two parameters (one which is optional).

alt text

Excel.Range.Value. The Value property can accept one parameter (which is optional).

alt text

Excel.Range.Value2. The Value2 property can accept two parameters (which are both optional).

alt text

Default Properties and Optional Parameters

Before trying to understand parameterized properties you need to have a good understanding of the following:
Default Properties - In VBA there are default properties but in C# you must be explicit.
The following two lines are equivalent because Value is the default property.

Worksheet.Range("A1") = "text" 
Worksheet.Range("A1").Value = "text"

The following two lines are equivalent because Item is the default property.

Worksheet.Range("A1:B10").Cells(5) 
Worksheet.Range("A1:B10").Cells.Item(5)

Optional Parameters - In VBA there are plenty of properties with optional parameters.
The following two lines are equivalent because xlRangeValueDefault is the default value for the optional parameter.

Worksheet.Range("A1").Value 
Worksheet.Range("A1").Value(xlRangeValueDataType.xlRangeValueDefault)

Range.Value2 - This property does not have any parameters unlike the Range.Value property
The only difference is that Value2 does not support the Currency and Date data types.
If values are formatted with either of these data types they are returned as Double.
In most cases the following two lines are equivalent.

Worksheet.Range("A1").Value = "text" 'in VBA  
Worksheet.Range("A1").Value2 = "text" 'in VBA

Indexer Syntax

Introduced in C# 4.0 / Visual Studio 2010 you can call parameterized properties using the Indexer syntax.
This feature just provides a shorthand syntax to the get and set accessor methods.
Any objects that have properties that accept parameters have been extended to use indexer syntax.
An indexer enables you to access the property in the same way you would access an array, ie by using square brackets instead of curved brackets.


The following code snippet shows how this is achieved in C#.

Excel.Worksheet myWsheet; 
Excel.Range myRange;
myWsheet = Globals.ThisAddIn.Application.ActiveSheet;
myRange = myWsheet.Range["A1"];
myRange = myWsheet.Range["A1","B1"];

//assign one range to another range
myWsheet.Range["A1"].Value = myWsheet.Range["B1"].Value;

//put text into a range
myWsheet.Range["A1"].Value = "text";

//pull text from a range (using cells)
string sText1 = myWsheet.Cells[1, 1].Value;

//access the 5th cell in a range
myRange = myWsheet.Range["A1:B10"].Cells.Item[5];

//display a built-in dialog box
Application.FileDialog[Office.msoFileDialogType.msoFileDialogFolderPicker].Show();

Get and Set Accessor Methods

You can call parameterized properties using the Get and Set accessor methods.
If you want to get the value of a parameterized property then you can use the method which has the "get_" prefix.
If you want to set the value of a parameterized property then you can use the method which has the "set_" prefix.

myRange = myWsheet.get_Range("A1"); 
myRange = myWsheet.get_Range("A1", System.Type.Missing);
myRange = myWsheet.get_Range("A1","B1");

//assign one range to another range
myWsheet.get_Range("A1").set_Value(myWsheet.get_Range("B1"));
            
//put text into a range
myWsheet.get_Range("A1").set_Value("text");
myWsheet.get_Range("A1", System.Type.Missing).set_Value("text");

//pull text from a range (using cells)
string sText2 = myWsheet.Cells.get_Item(1,1).get_Value();

//access an individual cell in a range
myRange = myWsheet.Range["A1:B10"].Cells.get_Item(5);

//display a built-in dialog box
Application.get_FileDialog(Office.msoFileDialogType.msoFileDialogFolderPicker).Show();

.NET Framework 3.5

Visual Studio 2008 included a set of additional assemblies called the Office Interop API Extensions
The Office Interop API Extensions are libraries that make working with optional parameters, variant types and parameterized properties easier in C#.
These were part of the VSTO Power Tools.


VSTO Document Level

A small number of the COM objects were accessible using the Indexer Syntax prior to C# 4.0 / Visual Studio 2010.
VSTO document-level projects included a number of objects that has been extended by VSTO.
These parameterized properties could be called using the Indexer Syntax.
One of these objects was the VSTO Range object.


Programming Word

There are far less parameterized properties in Word than Excel.
In Word the get_ and set_ accessor methods are hidden and do not appear in the intellisense or the object browser.
The get and set accessor methods accept Variant parameters.
For example the Style property is one such property.

Word.Range objRange 
Object objStyle = "Normal";

objRange.set_Style(ref objStyle);
objRange.get_Style();

object objFile = "myfile.doc"; 
Application.Documents.get_Item(ref objFile).Save();

You can pass either a string or an integer to this method because C# can automatically convert value types to object types.
This automatic conversion is called "boxing".
An instance of an object called a box is created to contain the value type.


Important

VB.Net (unlike C#) supports default properties.
VB.Net (unlike C#) supports parameterized properties.
For more information about C# indexers, please refer to the Classes > Indexed Properties page.


© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopPrevNext