I'm not sure what I'm doing wrong here. I have a generic class, which is basically an illustrious integer, with several methods for specific formatting strings, as well as in / out string conversion and int:
public class Base { protected int m_value; ... // From int public static implicit operator Base(int Value) { return new Base(Value); } ... // To string public static explicit operator string(Base Value) { return String.Format("${0:X6}", (int)Value); } }
And it works great. I can successfully use implicit and explicit conversions:
Base b = 1; Console.WriteLine((string)b); // Outputs "$000001", as expected.
Then I get different child classes from this class that turn on / off different named bits in m_value. For example:
public class Derived : Base { }
And then I can not use my implicit conversions to / from int:
Derived d = 3;
Even this gives the same error:
Derived d = (int)3;
Are implicit / explicit conversions not inherited in a derived class? This will require a lot of copy code, if not.
REACTION Thank you so much for the quick answers! You all deserve the mark "answer", they are all very good answers. The key is to think of types on either side of the equal sign. Now that I think about it, it makes sense.
Obviously, I only need to rewrite my conversions "into derivatives." Conversions "to Int32, String, etc." Still applicable.
inheritance c #
Jonathon reinhart
source share