Kapil Sen
Kapil Sen

Reputation: 3

overloading method with different return type with namespace not reference in another project consuming it

Project A has few overloaded methods to return XMLNode and MSXML2.IXMLDOMNode with but different parameters as follows

static public XmlNode XMLNewChildNode(XmlNode oParent, string sName)
    {
        ...
        return xmlNode;
    }

and

static public MSXML2.IXMLDOMNode XMLNewChildNode(MSXML2.IXMLDOMNode oParent, string sName)
    {
        ...
        return ixmldomnode;
    }

Project B doesn't have reference to interop.MSXML2.dll and when I consude XMLNewChildNode() to return XMLNode, I get compile error.

XmlNode oNode = XMLHelper.XMLNewChildNode2((XmlNode)oDoc, UCMCommonPTLIndep.gsUCP_DDT_ROOT_NODE);

Error: The type 'MSXML2.IXMLDOMNode' is defined in an assembly that is not referenced. You must add a reference to assembly 'Interop.MSXML2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=98ec9cb7b15b7e98'.

Is not possible to have overloads like this in c#? Am I missing somthing?

Upvotes: 0

Views: 310

Answers (1)

JLRishe
JLRishe

Reputation: 101738

"Is not possible to have overloads like this in c#?"

Clearly it is, as long as the consumers of those overloads have the needed references.

You should be able to resolve this by giving the methods different names, but preferably, you should put the methods in different classes. This would also resolve your issue:

public static class XmlNodeHelper
{
    static public XmlNode XMLNewChildNode(XmlNode oParent, string sName)
    {
        ...
        return xmlNode;
    }
}

public static class MSXMLHelper
{
    static public MSXML2.IXMLDOMNode XMLNewChildNode(MSXML2.IXMLDOMNode oParent, string sName)
    {
        ...
        return ixmldomnode;
    }
}

Upvotes: 2

Related Questions