Is there a way to change the order of constructors listed in IntelliSense in Visual Studio?
Asked Answered
L

5

13

I have defined a class with multiple constructors so that the underlying interfaces are immutable once the object is instantiated. I would like one of the constructors to be the "default" constructor for when a user types the following in Visual Studio:

var obj = new MyClass(

Dim obj As New MyClass(

Currently when I go to instantiate the object, the constructors aren't listed (in Visual Studio IntelliSense) in the order I declared them in my class. Is there a way to mark up my constructors so that their methods appear in a particular order during instantiation in Visual Studio IntelliSense?

Locklear answered 2/8, 2010 at 20:5 Comment(0)
B
13

There isn't a way to control the ordering within Visual Studio's Intellisense. If you do have multiple constructors (or methods), your only real control in terms of intellisense is to use EditorBrowsable with the appropriate EditorBrowsableState. This allows you to hide a contructor (or method) in intellisense, or only have it displayed in "advanced" mode, but does not allow you to reorder them.

However, in this situation, if you're targetting .NET 4, I'd recommend considering using a single constructor, and taking advantage of named and optional arguments.

Belorussia answered 2/8, 2010 at 20:10 Comment(1)
great suggestion on using named and optional arguments. Because of that, I was able to reduce the number of constructors from 4 to 2.Locklear
M
4

That's an interesting question, but I haven't heard of such capability. One option would be marking the other constructors as advanced or hidden.

ComponentModel.EditorBrowsable( ComponentModel.EditorBrowsableState.Advanced ) ComponentModel.EditorBrowsable( ComponentModel.EditorBrowsableState.Never )

Monserratemonsieur answered 2/8, 2010 at 20:11 Comment(1)
Great suggestion, but this doesn't seem to work on constructors for some reason.Cursor
O
2

I'm pretty sure what you are asking for is impossible. The best thing to do is to mark up your constructors with XML comments which will be used to populate the intellisense in VS. That will give the user detailed intellisense about which constructors is default etc.

Editorial: I'm pretty sure VB.NET orders the constructors in the order of their present in the class declaration.

You have to remember that Intellisense isn't a feature of the language, but of the editor. You wouldn't have features specific to the IDE built into the language because other editors can be used to write code. Writing code for the purpose of writing code is off the mark.

Objective answered 2/8, 2010 at 20:11 Comment(0)
S
0

You could always use a Factory model and make the constructor protected for each method. However I perfer Reed Copsey answer. However if .NET 4.0 is not an option this could be an alternative.

Sanious answered 2/8, 2010 at 20:11 Comment(0)
C
0

It looks like there still is no way to dictate the order as an application developer. It appears IntelliSense sorts first on the number of arguments, disregarding whether an argument is optional or not (it counts either way).

If you have optional arguments you can rework them to overloads though, thus creating a new method/constructor with less parameters which will then appear earlier. You can also hide constructors that are useless to an application developer by declaring them internal.

Using these methods I could fix my little problem. I had a deserializing constructor that popped up before the more commonly used one.

Chkalov answered 5/12, 2021 at 12:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.