Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# double standard?

Tags:

c#

interface

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.");
    }
}
like image 324
Jasmine Avatar asked Dec 09 '22 22:12

Jasmine


2 Answers

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.

like image 70
recursive Avatar answered Dec 20 '22 09:12

recursive


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 msdn1.

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.");
    }
}
like image 27
Chris Martin Avatar answered Dec 20 '22 11:12

Chris Martin