VariableCollection Class

A collection of document variables.

Inheritance Hierarchy

SystemObject
  Aspose.WordsVariableCollection

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.7.0

Syntax

public class VariableCollection : IEnumerable<KeyValuePair<string, string>>, 
	IEnumerable

The VariableCollection type exposes the following members.

Properties

  NameDescription
Public propertyCode exampleCount
Gets the number of elements contained in the collection.
Public propertyItemInt32
Gets or sets a document variable at the specified index. null values are not allowed as a right hand side of the assignment and will be replaced by empty string.
Public propertyItemString
Gets or a sets a document variable by the case-insensitive name. null values are not allowed as a right hand side of the assignment and will be replaced by empty string.

Methods

  NameDescription
Public methodCode exampleAdd
Adds a document variable to the collection.
Public methodCode exampleClear
Removes all elements from the collection.
Public methodCode exampleContains
Determines whether the collection contains a document variable with the given name.
Public methodEquals (Inherited from Object.)
Public methodCode exampleGetEnumerator
Returns an enumerator object that can be used to iterate over all variable in the collection.
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Public methodCode exampleIndexOfKey
Returns the zero-based index of the specified document variable in the collection.
Public methodCode exampleRemove
Removes a document variable with the specified name from the collection.
Public methodCode exampleRemoveAt
Removes a document variable at the specified index.
Public methodToString (Inherited from Object.)

Remarks

Variable names and values are strings.

Variable names are case-insensitive.

Examples

Shows how to work with a document's variable collection.
Document doc = new Document();
VariableCollection variables = doc.Variables;

// Documents have a variable collection to which name/value pairs can be added
variables.Add("Home address", "123 Main St.");
variables.Add("City", "London");
variables.Add("Bedrooms", "3");

Assert.AreEqual(3, variables.Count);

// Variables can be referenced and have their values presented in the document by DOCVARIABLE fields
DocumentBuilder builder = new DocumentBuilder(doc);
FieldDocVariable field = (FieldDocVariable)builder.InsertField(FieldType.FieldDocVariable, true);
field.VariableName = "Home address";
field.Update();

Assert.AreEqual("123 Main St.", field.Result);

// Assigning values to existing keys will update them
variables.Add("Home address", "456 Queen St.");

// DOCVARIABLE fields also need to be updated in order to show an accurate up to date value
field.Update();

Assert.AreEqual("456 Queen St.", field.Result);

// The existence of variables can be looked up either by name or value like this
Assert.True(variables.Contains("City"));
Assert.True(variables.Any(v => v.Value == "London"));

// Variables are automatically sorted in alphabetical order
Assert.AreEqual(0, variables.IndexOfKey("Bedrooms"));
Assert.AreEqual(1, variables.IndexOfKey("City"));
Assert.AreEqual(2, variables.IndexOfKey("Home address"));

// Enumerate over the collection of variables
using (IEnumerator<KeyValuePair<string, string>> enumerator = doc.Variables.GetEnumerator())
    while (enumerator.MoveNext())
        Console.WriteLine($"Name: {enumerator.Current.Key}, Value: {enumerator.Current.Value}");

// Variables can be removed either by name or index, or the entire collection can be cleared at once
variables.Remove("City");

Assert.False(variables.Contains("City"));

variables.RemoveAt(1);

Assert.False(variables.Contains("Home address"));

variables.Clear();

Assert.That(variables, Is.Empty);

See Also