Friday, September 10, 2004

Win32 vs. .NET casting issues in Delphi

In a small series of blog articles I'm planning to talk about some of the different types of casts and conversions that Delphi supports, and mention any differences in casting logic and implementation between Win32 and .NET.

In general, using casts in your code indicates an impendence mismatch between your design and the problem domain. Sometimes they cannot be easily avoided, other times it might be better to refactor your design and code to remove or reduce the number of casts. Until true generic classes become available (as they probably will - at least for .NET 2.0), it is close to impossible to have no casts anywhere in the code. They can (and should) be encapsulated to a few core locations, however - typically in type-specific collection classes.

For the purposes of this discussion, I've identified 9 separate categories of casts, in terms of the types of the entities we are casting to and from.

Types of casts:

1. Object-to-object upcast

2. Object- to-object downcast

3. Object-to-interface cast

4. Interface-to-interface cast

5. Interface-to-object cast

6. Valuetype-to-valuetype cast

7. Valuetype-to-object cast

8. Object-to-valuetype cast

9. Casts to and from compiler-managed types

The upcoming blog posts will each discuss one or two of these casting categories.

No comments:

Copyright © 2004-2007 by Hallvard Vassbotn