user175084
user175084

Reputation: 4640

how to overload two methods with different input parameters

I have these two functions

private void calcResults()
{
   MakePath(id, results, _resultCount);
   MakePath(id, "XYZ", _resultSICount)
}

private string MakePath(string subFolder, object obj, int index)
{
    string dir = System.IO.Path.Combine(_outputDir, subFolder);
    string fileName = string.Format("{0} {1} {2}.xml",
           obj.GetType().Name, _dateTimeSource.Now.ToString(DATE_FORMAT), index.ToString());
    return System.IO.Path.Combine(dir, fileName);
}

private string MakePath(string subFolder, string tempFileName, int index)
{
    string dir = System.IO.Path.Combine(_outputDir, subFolder);
    string fileName = string.Format("{0} {1} {2}.xml",
           tempFileName, _dateTimeSource.Now.ToString(DATE_FORMAT), index.ToString());
    return System.IO.Path.Combine(dir, fileName);
}

Please can some one help.

Thanks

Upvotes: 0

Views: 9328

Answers (5)

dotcomslashnet
dotcomslashnet

Reputation: 148

If I understand your question correctly, you mean you want to overload the method to avoid code duplication... here's how I'd go about it.

    private void calcResults()
    {
       MakePath(id, results.GetType(), _resultCount);
       MakePath(id, "XYZ", _resultSICount)
    }

    private string MakePath(string subFolder, Type type, int index)
    {
        return MakePath(subFolder, type.Name, index);
    }

    private string MakePath(string subFolder, string tempFileName, int index)
    {
        string dir = System.IO.Path.Combine(_outputDir, subFolder);
        string fileName = string.Format("{0} {1} {2}.xml",
               tempFileName, _dateTimeSource.Now.ToString(DATE_FORMAT), index.ToString());
        return System.IO.Path.Combine(dir, fileName);
    }

I would avoid using object as the type for your second parameter, as it seems ambiguous in this case, using Type indicates the intended purpose of the parameter.

Upvotes: 8

Erwin
Erwin

Reputation: 3090

You could cast the 'results' value to an object:

class Program
{
    static void Main(string[] args)
    {

        var ret1 = GetValue("String");
        Console.WriteLine(ret1);
        var ret2 = GetValue((object)"test");
        Console.WriteLine(ret2);

        Console.ReadKey();
    }

    private static object GetValue(string p0)
    {
        return p0;
    }

    private static object GetValue(object p0)
    {
        return "Object";
    }
}

Second call to GetValue get's routed to the one with the object param.

Upvotes: 0

David
David

Reputation: 16287

private string MakePath(string subFolder, object obj, int index)
{
    if(obj.GetType()==typeof(string))
    {
        //copy the strong typed version to here;
        return;
    }
    string dir = System.IO.Path.Combine(_outputDir, subFolder);
    string fileName = string.Format("{0} {1} {2}.xml",
        obj.GetType().Name, _dateTimeSource.Now.ToString(DATE_FORMAT), index.ToString());
    return System.IO.Path.Combine(dir, fileName);
}

You can then use one function to do two jobs. Will this do?

Upvotes: 0

alex
alex

Reputation: 12654

The problem here is that string is also an object, so compiler cannot choose what method to use. You can change parameter order, or rename one of the methods.

Upvotes: 0

Tigran
Tigran

Reputation: 62265

You can think about something like this:

    private string MakePath(string subFolder, object obj, int index)
    {        
        //tempFileName here is created beased on the TYPE of the object passed
        string tempFileName = obj.GetType().Name;
        return MakePath(subFolder,tempFileName , index); 

    }

    private string MakePath(string subFolder, string tempFileName, int index)
    {
        //combine directory path
        string dir = System.IO.Path.Combine(_outputDir, subFolder);

        //compute final file name based on the several 
        //parameters and tempFileName parameter
        string fileName = string.Format("{0} {1} {2}.xml",
            tempFileName, _dateTimeSource.Now.ToString(DATE_FORMAT), index.ToString());

        return System.IO.Path.Combine(dir, fileName);
    }

Following the logic of the code presented, correct me if I'm wrong, the only difference between these 2 methos is that in first tempFileName is based on the type name, in the second, instead, it's a just a second parameter passed by the caller.

Upvotes: 2

Related Questions