views:

115

answers:

4

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?

+8  A: 

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.

Reed Copsey
@Reed great suggestion on using named and optional arguments. Because of that, I was able to reduce the number of constructors from 4 to 2.
Ben McCormack
+1  A: 

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.

Achilles
+2  A: 

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 )

Jonathan Allen
A: 

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.

David Basarab