Reputation: 1332
How to get around? I do not want to have functions with different names.
public class DataRowSafe
{
public String Get(String Column)
{
return String.Empty;
}
public int Get(String Column)
{
return 0;
}
}
DataRowSafe r=new DataRowSafe();
String res=r.Get("Column1");
int res2=r.Get("Column2");//<--- Ambiguous call
Upvotes: 1
Views: 118
Reputation: 4981
It is not possible, because overloading works only for different signatures. If signatures are the same then c# compiler will return error.
Upvotes: 0
Reputation: 8937
You can you referenced parameners like this instead
public class DataRowSafe
{
public void Get(String Column, ref string myParam)
{
myParam = String.Empty;
}
public void Get(String Column,ref int myParam)
{
myParam = 0;
}
}
int i = 0;
string st = "";
new DataRowSafe().Get("name", ref i);
new DataRowSafe().Get("name", ref st);
Upvotes: 2
Reputation: 38200
you should be getting an error like
'DataRowSafe' already defines a member called 'Get' with the same parameter types
The return type of the function is not significant but in this case the compiler is confused with the two method available for call and not sure which is to be picked up maybe you could use generics to overcome this
a sample like
public static T GetValue<T>(string column)
{
string returnvalue="";
//process the data ...
return (T)Convert.ChangeType(returnvalue, typeof(T), CultureInfo.InvariantCulture);
}
Upvotes: 1
Reputation: 3794
You can't, there is no way, the only way would be to have a different signature.
Upvotes: 2