Reputation: 25928
I am getting the compile error
The call is ambiguous between the following methods or properties getXmlNodeValue.
I have 3 methods all with the same name and parameters, these 3 methods do however have 3 different return types (double
, int
and string
). Is it possible to have 3 methods with the same name and parameters but different return types?
If not, what method would you suggest I use? Ie, just change the method names to getXmlNodeText
, getXmlNodeDouble
, etc. or something else?
private static string getXmlNodeText(XmlNode node, string xPath, XmlNamespaceManager nsmgr)
{
try
{
return node.SelectSingleNode(xPath, nsmgr).InnerText;
}
catch (Exception e)
{
return string.Empty;
}
}
public static string getXmlNodeValue(XmlNode node, string xPath, XmlNamespaceManager nsmgr)
{
return getXmlNodeText(node, xPath, nsmgr);
}
public static double getXmlNodeValue(XmlNode node, string xPath, XmlNamespaceManager nsmgr)
{
return Convert.ToDouble(getXmlNodeText(node, xPath, nsmgr));
}
public static int getXmlNodeValue(XmlNode node, string xPath, XmlNamespaceManager nsmgr)
{
return Convert.ToInt32(getXmlNodeText(node, xPath, nsmgr));
}
// Usage problem:
string name = getXmlNodeValue(pitNode, "ns:name", nsmgr);
Upvotes: 0
Views: 1283
Reputation: 169
use generics as shown below to get the desired type value.
static void Main(string[] args)
{
int value1 = GetValue<Int16>("23");
double value2 = GetValue<double>("23.34");
}
public static T GetValue<T>(string value)
{
return (T)Convert.ChangeType(value, typeof(T));
}
Upvotes: 0
Reputation: 39248
No, different return type does not constitute an overload. You're better off renaming the methods with a convention that includes the return type name.
Another approach is to use generics to make the typing flexible. More info here http://msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx
Upvotes: 1