Are Odd / explicit inheritance translation methods in C #?

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; // Cannot implicitly convert type 'int' to 'Derived'. An explicit conversion exists (are you missing a cast?) 

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.

+7
inheritance c #
source share
6 answers

Cause

 Derived d = (int)3; 

does not work, because the Derived type does not exactly match the return value of the Base statement, which is required to invoke this statement. Note that you did not provide any conversion statements containing the new Derived(...) code, so it is not surprising that you cannot create new Derived instances this way.

Note, however, that the inverse transform

 Derived v = ...; string s = (string)v; 

will work fine (as if it had been "inherited", although this is actually not inheritance due to the static ).

+6
source share

No, that will not work. The compiler will not explicitly hide from the database to the output for you. Basically, you cannot ...

 D d = new B(); 

You will get the cast of the base class from your string, because it will do an implicit raise for you.

You can work if you don't want to copy your methods into a derived class using the extension function for integers (for example, if your derived class is called D) ...

 public static class Ext { public static D IntAsD(this int val) { return new D(val); } } 

Then you can do what you want ...

 D d1 = 5.IntAsD(); 

Of course, this is not ideal, but it may suit your needs.

+3
source share

Overloaded operators (including transformations) are inherited, but not in the way you seem to expect them. In particular, they are not magically extended to work on derived types. Let's look at your code again:

 Derived d = 3; 

At this point, the compiler has two classes for working with System.Int32 and Derived. Therefore, he is looking for conversion operators in these classes. He will consider those that are inherited by Derived from Base, namely:

 public static implicit operator Base(int Value); public static explicit operator string(Base Value); 

The second does not match, because the argument must be Int32. The first corresponds to the argument, but then it returns the Base type, and that does not match. Alternatively, you can write this:

 string s = (string) new Derived(); 

And it will work because an explicit statement in Base exists and is applicable (it is expected that the Base, Derived argument is inherited from Base and therefore matches the type of the argument).

The reason that the compiler will not automatically override the conversion operators in derived types "correctly" is because there is no general way to do this.

+2
source share

Conversion / operator methods are convenience, but as others have noted, they will not always do what you want. In particular, if the compiler does not see the correct types on both sides of the = sign, it will not even consider your conversion / operator. This is why, if you do the following, you get a compiler warning:

 object str = "hello"; if ( str == "hello" ) { } 

In addition, custom conversions are not taken into account when using the as / keywords.

When you are considering implementing an explicit / implicit conversion, you should also consider implementing IConvertible and / or implementing TypeConverter. This gives you great flexibility when converting base class descriptor types.

+1
source share

No, it is not. Here is the key:

public **static** implicit operator Base(int Value)

static methods in C # are just global functions with access to private members of a class. They are never inherited.

0
source share

While you cannot do this directly, there are several short cuts you can use to enable code reuse.

 public class Base { protected int m_value; ... // From int public static implicit operator Base(int Value) { Base newObject = new Base(); newObject.FromInt(Value); return newObject; } ... // To string public static explicit operator string(Base Value) { return String.Format("${0:X6}", (int)Value); } //Instance FromInt() protected void FromInt(int value) { m_value = value; } } public class Derived : Base { // To string, re-use Base-ToString public static explicit operator string(Derived Value) { // Cast (Derived)Value to (Base)Value and use implemented (Base)toString return (string)(Base)Value; } // Use FromInt, which has access to instance variables public static implicit operator Base(int Value) { // We can't use (Base)obj = Value because it will create // a "new Base()" not a "new Derived()" Derived newObject = new Derived(); newObject.FromInt(Value); return newObject; } } 
0
source share

All Articles