tags:

views:

475

answers:

6

I am doing the exact same thing in two classes, and in one the compiler is allowing it just fine, but the other one is giving me an error. Why the double standard? There's 15 classes using this same pattern, but only one refuses to compile, saying the following error:

'AWWAInvoicingXML.AwwaTransmissionInfo' does not implement interface member 'AWWAInvoicingXML.IXmlSerializable.fromXML(System.Xml.XmlDocumentFragment)'. 'AWWAInvoicingXML.AwwaTransmissionInfo.fromXML(System.Xml.XmlDocumentFragment)' is either static, not public, or has the wrong return type.

Here is my source code... if I comment out the AwwaTransmissionInfo class, the rest of the file compiles just fine, so I know it's not one of those where the compiler is just dying after the first error. And I know, I know, there's built-in stuff for what I'm trying to do here, but just assume I actually know what I'm doing and skipped the built-in serializers for a reason :)

    public interface IXmlSerializable {
 //if this interface is implemented, the object can be serialized to XML
 string toXML();
 IXmlSerializable fromXML(XmlDocumentFragment inXml);
}

public class AwwaTransmissionInfo : IXmlSerializable {

 public DateTime DateTime = DateTime.Now;
 public int ItemCount;

 public string toXML() {
  throw new Exception("The method or operation is not implemented.");
 }

 public AwwaTransmissionInfo fromXML(XmlDocumentFragment inXml) {
  throw new Exception("The method or operation is not implemented.");
 }

}

public class CEmail {
 public string Email = "";

 public string toXML() {
  throw new System.Exception("The method or operation is not implemented.");
 }

 public CEmail fromXML(XmlDocumentFragment inXml) {
  throw new System.Exception("The method or operation is not implemented.");
 }
}
+1  A: 

AwwaTransmissionInfo.fromXml needs to be returning an IXmlSerializable type, not an AwwaTransmissionInfo.

Andy
Yes, but as you can see AwwaTransmissionInfo implements IXmlSerializable, so it SHOULD work...
BFree
"Rob" posted the correct answer first... I forgot to implement the interface. This is a dumb question - I really messed up. I'm under a little pressure today :)
Jasmine
Also I voted this one down, because as you can see, the function in both classes is returning a "self" type.
Jasmine
A: 

Your CEmail class is not implementing the IXmlSerializable interface at all. Implement it and also change the return type of both your CEmail and AWwaTransmissionInfo fromXML to IXmlSerializable.

public IXmlSerializable fromXML(XmlDocumentFragment inXml){
   throw new System.Exception("The method...");
}
Dan Appleyard
+10  A: 

The problem is that the method signature must match the interface exactly.

The easiest solution is to change

    public AwwaTransmissionInfo fromXML(XmlDocumentFragment inXml) {

to

    public IXmlSerializable fromXML(XmlDocumentFragment inXml) {

If you are unhappy with that, you can implement the interface explicitly. Add this:

    public IXmlSerializable IXmlSerializable.fromXML(XmlDocumentFragment inXml) {
        return this.fromXML(inXml);
    }

Then you will have two definitions for fromXML(), one for use when being called as as instance of the class, and one for use when being called through the interface.

recursive
A: 

The "fromXml" method on the derived AwwaTransmissionInfo class is returning AwwaTransmissionInfo type, where as the interface defines that the method should return the IXmlSerializable type. Since AwwaTransmissionInfo derives from IXmlSerializable, you'd think, oh, cool, that works, but it doesn't on an interface. I think that would be variance (contra- or co-, i can never remember which one)??

If you were to refactor this using generics, then you could use a constraint that would allow you to get around some of this.

joshua.ewer
holy crap, you are all fast! ;-)
joshua.ewer
+2  A: 

IXmlSerializable already exists in the .NET framework. I would advise you to implement that before going off on your own and re-inventing the wheel.

See msdn[1].

As to the reason your code won't compile:

The method fromXml needs to return IXmlSerializable.

public IXmlSerializable fromXML(XmlDocumentFragment inXml)
{
    throw new Exception("The method or operation is not implemented.");
}

If you want to return something else, consider using a generic interface.

IE.

public interface IXmlSerializable<T>
{
    //if this interface is implemented, the object can be serialized to XML
    string toXML();
    T fromXML(XmlDocumentFragment inXml);
}

public class AwwaTransmissionInfo : IXmlSerializable<AwwaTransmissionInfo>
{

    public DateTime DateTime = DateTime.Now;
    public int ItemCount;

    public string toXML()
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public AwwaTransmissionInfo fromXML(XmlDocumentFragment inXml)
    {
        throw new Exception("The method or operation is not implemented.");
    }
}
Chris Martin
DUH - maybe you might read the question next time. My problem here is even dumber than not using built-in stuff.
Jasmine
Yeah and you're a dick. Hopefully I'll remember to not try and help you in the future.
Chris Martin
I specifically mentioned that I know about that... but I like your idea about the generic, so I changed my vote. Thanks :)
Jasmine
+1  A: 

The reason the CEmail class was compiling fine is because it was not marked as implementing the interface. When I marked it as such, I got the same error as the other class. The following is how it has to be... I am pretty stupid for not noticing that. We are under a lot of pressure here right now and I can't believe I stared at this code for like an hour before I posted a question, and it was something so simple... it always is I guess :)

    public interface IXmlSerializable {
 //if this interface is implemented, the object can be serialized to XML
 string toXML();
 IXmlSerializable fromXML(XmlDocumentFragment inXml);
}

public class AwwaTransmissionInfo : IXmlSerializable {

 public DateTime DateTime = DateTime.Now;
 public int ItemCount;

 public string toXML() {
  throw new Exception("The method or operation is not implemented.");
 }

 public IXmlSerializable fromXML(XmlDocumentFragment inXml) {
  throw new Exception("The method or operation is not implemented.");
 }

}

public class CEmail : **IXmlSerializable** {
 public string Email = "";

 public string toXML() {
  throw new System.Exception("The method or operation is not implemented.");
 }

 public IXmlSerializable fromXML(XmlDocumentFragment inXml) {
  throw new System.Exception("The method or operation is not implemented.");
 }
}
Jasmine