So far, we have discussed the properties that return one of the base types Node or CompositeNode. You will have noticed that you might have to cast the values to the concrete class of the node, such as Run or Paragraph.
Many casting or explicit conversions between types using the as operator is often considered a bad smell in an object oriented code. However, casting is not always bad; sometimes a bit of casting is necessary. We found you cannot completely get away without casting when working with an object model that is a Composite, like the Aspose.Words DOM.
To reduce the need for casting, most of the Aspose.Words classes provide properties and collections that allow strictly typed access. There are three basic patterns for typed access:
- A parent node exposes typed FirstXXX and LastXXX properties. For example, Document has FirstSection and LastSection properties. Similarly, Paragraph has FirstRun and LastRun properties and so on.
- A parent node exposes a typed collection of child nodes, for example Document.Sections, Body.Paragraphs and so on.
- A child node provides typed access to its parent, for example Run.ParentParagraph, Paragraph.ParentStory etc.
Typed properties are merely useful shortcuts that sometimes allow easier access than the generic properties inherited from Node and CompositeNode.
Example TypedPropertiesAccess
Demonstrates how to use typed properties to access nodes of the document tree.
[C#]
// Quick typed access to the first child Section node of the Document.
Section section = doc.FirstSection;
// Quick typed access to the Body child node of the Section.
Body body = section.Body;
// Quick typed access to all Table child nodes contained in the Body.
TableCollection tables = body.Tables;
foreach (Table table in tables)
{
// Quick typed access to the first row of the table.
if (table.FirstRow != null)
table.FirstRow.Remove();
// Quick typed access to the last row of the table.
if (table.LastRow != null)
table.LastRow.Remove();
}
[Visual Basic]
' Quick typed access to the first child Section node of the Document.
Dim section As Section = doc.FirstSection
' Quick typed access to the Body child node of the Section.
Dim body As Body = section.Body
' Quick typed access to all Table child nodes contained in the Body.
Dim tables As TableCollection = body.Tables
For Each table As Table In tables
' Quick typed access to the first row of the table.
If Not table.FirstRow Is Nothing Then
table.FirstRow.Remove()
End If
' Quick typed access to the last row of the table.
If Not table.LastRow Is Nothing Then
table.LastRow.Remove()
End If
Next table
[Java]
// Quick typed access to the first child Section node of the Document.
Section section = doc.getFirstSection();
// Quick typed access to the Body child node of the Section.
Body body = section.getBody();
// Quick typed access to all Table child nodes contained in the Body.
TableCollection tables = body.getTables();
for (Table table : tables)
{
// Quick typed access to the first row of the table.
if (table.getFirstRow() != null)
table.getFirstRow().remove();
// Quick typed access to the last row of the table.
if (table.getLastRow() != null)
table.getLastRow().remove();
}