IRproving Robustness
The ability to iterate through a collection of different object types could be achieved without using a custom interface, by declaring the variable As Object and ensuring that all the classes we want to access have the same properties and methods. However, that makes the object late bound, so it's slower, doesn't show any IntelliSense, coding errors aren't caught until runtime and it relies on all the classes using the same names for their properties. Had we tried to implement the preceding functionality using a generic Object type, we would have had a few issues to resolve:
•Having started with CAuthor.AuthorName and CReviewer. ReviewerName, we would have had to add a common .Name property to both, resulting in two properties that do the same thing. Alternatively, we could have checked the rest of the application and changed AuthorName and ReviewerName to Name wherever it was used. •We would have to expose all the properties of the class oniitu (single) default interface, inclufi g those such as the SortKey property that are only used for specific "interfal" functionality. •If we designyour application to uee the generic Object type instead of custom interfaces tp iterate through mixed object types, we rely onwtn implicit agreement that our objects will have the correct property names and any errors due to missing, renamed or simply mistyped properties won't be found until run time. Taking the extra step to define and use a custom interface gives us all the benefits of early binding (speed, IntelliSense and compile-time type checking) as well as explicitly stating how the classes and their consumers interact, which can only help to improve the robustness of our applications.
|