blah blah blah is here! blah blah » Close

foamy
2499
426 views

C# overloads

up1down
link

Hi all,

I'm trying to make some overloads of some of my methods in my "toolbox" library. I have a method called ProcCheck that I want to be able to use with these overloads:

public static bool ProcCheck(string processName)
{
return ProcCheck(processName, "", "");
}

public static bool ProcCheck(string processName, string userName)
{
return ProcCheck(processName, userName, "");
}

public static bool ProcCheck(string processName, string machineName)
{
return ProcCheck(processName, "", machineName);
}


This will obviously not compile since two of the signatures are identical. My question is: What is a good way to achieve these two overloads without simply renaming one of them?

last answered one year ago

2 answers

up1down
link

In versions of .NET up to 3.5, the only sensible approach IMO is to accept that the third overload isn't feasible and that you'll therefore have to call ProcCheck(processName, "", machineName) directly.

In .NET 4.0, you could use optional parameters to combine ALL the overloads into one:

public static bool ProcCheck(string processName, string userName = "", string machineName = "") // specify default values for optional parameters
{
/*
code to do the equivalent of
ProcCheck(processName, userName, machineName);
and return the result
*/
}

// can now call this method with any of the following
ProcCheck(processName);
ProcCheck(processName, userName);
ProcCheck(processName, machineName : machineName); //using 'named' parameter
ProcCheck(processName, userName, machineName);

foamy
2499

Well isn't that handy :) Now all that's left is to convince my manager that I need Visual Studio 2010 for this xD

vulpes
17279

If you're writing a library for other people to use, then I wouldn't use optional parameters anyway. They're not CLS compliant and, if you change the default values, client programs need to be recompiled. The main reason they were introduced is to facilitate COM or Office interop which is now hugely simplifed compared to what we had to do before :)

foamy
2499

I see. I was planning on using it for my own "toolbox" DLL so that wouldn't be a problem. However, I think I'll stick with renaming some of the overloads for now :)

IF you willing to use vs2010 express it should be ok.

up0down
link

Here is another method of doing it.
extending DotNets string rather
1. create a class like so

using System;
using System.Text;
using System.Security.Cryptography;

namespace Extensions.String
{
public static class StringExtension
{
public static bool ProcCheck(this string processName, string userName = "", string machineName = "")
{
return true;
}
}
}


you can use this class like so

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Extensions.String;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
"Notepad".ProcCheck();
"Notepad".ProcCheck("userName");
"Notepad".ProcCheck("userName", "machineName");
}
}
}

Feedback