views:

450

answers:

4

Hi everyone, I have two methods that are overloads of each other

public class Car
{
   public int GetPrice(string vinNumber)
   {
      string make = Database.GetMake(vinNumber);  // expensive operation
      string model = Database.GetModel(vinNumber);   // expensive operation
      int year = Database.GetYear(vinNumber);   // expensive operation

      return this.GetPrice(make, model, year);
   }

   public int GetPrice(string make, string model, int year)
   {
      // Calculate value and return
   }
}

In my example, the GetPrice(make, model, year) overload is cheap to execute but the GetPrice(vinNumber) method is expensive. The problem is that the expensive method has the fewest parameters and it shows up first in the C# intellisense.

Both methods are valid, but I want to encourage people to call the cheap method. But people tend to not look through all the overloads in Intellisense before choosing a method to call, and the expensive one is being called too often in my company's codebase.

Is there a way to tell Visual Studio to give "intellisense priority" to a particular method so it shows up first?

A: 

Don't think so.

Unless you write a intellisense plugin ( like Resharper) and hijack the default intellisense and create a program for users to assign the priority.

Ngu Soon Hui
Well, I assume he's getting at something like a [property] to define in the source file to "hint" Intellisense as to which overload takes priority.
Crowe T. Robot
A: 

What happens is this:

  • As you type the member or highlight it in the list, the single overload you see is the one listed first in the code.
  • After you accept the member and are inside the parentheses, the order appears to be based on number of parameters, from fewest to most.

What you might consider doing is, instead of overloads, naming the members the same at the beginning and different at the end (GetMake vs GetMakeSlow, but obviously something better than that) so they show up together in Intellisense but it's communicated which you should use.

Otherwise, make them true overloads but use XML documentation to put a clear warning on the slow one.

Kyralessa
That's what I thought I remembered as well but a quick test shows less params = first.
Cory Charlton
A: 

Only solution I can offer is comments but it doesn't mean the user will pay attention to them:

    /// <summary>
    /// This method should be used as a last resort...
    /// </summary>
    /// <param name="vinNumber"></param>
    /// <returns></returns>
    public int GetPrice(string vinNumber)
    {
        ...
    }

    /// <summary>
    /// This is the preferred method...
    /// </summary>
    /// <param name="make"></param>
    /// <param name="model"></param>
    /// <param name="year"></param>
    /// <returns></returns>
    public int GetPrice(string make, string model, int year)
    {
        ...
    }

Edit: I tried this an it didn't make any difference:

class Class1
{
    public static void Method(int value1) { }
    public static void Method(int value1, int value2) { }
    public static void Method(int value1, int value2, int value3) { }
}

class Class2
{
    public static void Method(int value1, int value2, int value3) { }
    public static void Method(int value1, int value2) { }
    public static void Method(int value1) { }
}
Cory Charlton
+1  A: 
  1. The Summary tag in XML comments shows up in Intellisense.
  2. You could decorate the method with the Obsolete tag, which will also generate a warning or error depending on settings.

    [System.Obsolete("use GetPrice(make, model, year)")]
    
phloopy
The summary shows up in Intellisense, but the remarks don't. (Both will show up in generated documentation.) Also, the #warning thing doesn't seem like a good option, because it shows up as a warning in the original method, not in the place where it's called.
Kyralessa
Kyralessa, I thought the remark tag was part of Intellisense but just tested and confirmed they're not. Good point on the warning, Obsolete is the way to go for that route. Thanks for the input, I edited my answer.
phloopy