You are throwing (making (Outlook.Items)oInbox.Items ). Casting means that you are referring to an object of type X as type Y This is true in the following scenarios:
X is inside the Y inheritance hierarchy (meaning that it is either the parent class Y or a child class Y ). In the case where X is the parent class, the cast will be executed only at runtime if the actual object has a value of Y (or a type derived from Y )Y is the type of interface that X implements.- There is an explicit conversion defined from
X to Y
Due to polymorphism, casting in the first case usually does not change the behavior of functions (although it can, if a more derived type explicitly hides the implementation of the parent). My suspicion, however, is that this is your scenario; type oInbox.Items is a type that inherits from Outlook.Items but hides the implementation of Outlook.Items.Sort . By explicitly dropping the parent type, you bypass the new child implementation. Please note that this technique only works when the child hides the function, and does not override the virtual function) .
The second case can change the behavior if X explicitly implements the function on Y that you are going to use. By choosing an interface, you explicitly tell the compiler that you want to associate the method call with the implementation of the interface, and not with the usual public-oriented method for the class itself.
The third almost always changes the behavior, since you get a completely different type (and, therefore, a completely different object).
I canโt say which of these cases applies to you, because I donโt have much experience with Office, but this should answer your main question: โHow could it be otherwise?โ
source share