[Mono-dev] Override casting behavior?
Arno Rehn
mono-devel at arnorehn.de
Mon Oct 19 17:10:47 EDT 2009
On Monday 19 October 2009 22:49:52 you wrote:
> Hi,
>
> I have a class A and a class B (both inherit from the same class, for what
> it's worth).
> I want to define special rules when casting from A to B and vice versa. Is
> it possible?
>
> For example, A would have 2 properties "Width" and "Height", while B would
> have a "Size" one. When casting from A to B, I'd like Size to be the
> smallest value of Width and Height. When casting from B to A, I'd like both
> Width and Height to have the Size value.
>
> I guess I could do this by making my own methods or special constructors,
> but I wanted to know if there was a transparent way to do it, by simply
> casting, like B b = (B)a;
>
> Looking on Google brought nothing, so either I missed it, or this isn't
> allowed because it'd be considered misleading, unintuitive or something?
> Any ideas?
Look for explicit and implicit conversion operators.
--
Arno Rehn
arno at arnorehn.de
More information about the Mono-devel-list
mailing list