Mixer
Mixer

Reputation: 1332

Ambiguous call functions in the class

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

Answers (5)

QArea
QArea

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

Alex
Alex

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

V4Vendetta
V4Vendetta

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

animaonline
animaonline

Reputation: 3794

You can't, there is no way, the only way would be to have a different signature.

Upvotes: 2

bash.d
bash.d

Reputation: 13207

The overloading of methods requires your similar-named methods to have different signatures. The return-value is insignificant! Have a look at this tutorial here.

Upvotes: 5

Related Questions