开发者

C# Source Code Formatting in array initializer

开发者 https://www.devze.com 2023-01-27 03:46 出处:网络
Is it possible to enable C# source code formatting inside array initializers in VS.NET 2010? Elements =

Is it possible to enable C# source code formatting inside array initializers in VS.NET 2010?

Elements =
{
// starting from here source code formatting does not work
  new TextViewModel("PublicId") 
  { 
    Label = new LText("Fund Nr:", "de", "Fondsnummer"),
    Watermark = new LText("Please enter fund number...", "de", "Bitte hier Fondsnummer erfassen...")
  },
  new CheckViewModel("Juristic")
  {
    Style = "Radi开发者_JS百科o",
    Default = true,
    Label = new LText("Juristic Fund", "de", "Juristischer Fonds")
  }
}    
// starting from here source code formatting works again

Am I missing some formatting option in Tools->Options->Text Editor->C#?


It's a "Won't fix" at connect.microsoft.

But, as posted there by Microsoft:

If others, find that this is an important feature, please keep voting it up.

Note that this is only for multi-line initializers.


Looks like this is a VS.NET "feature"


That doesn't look like valid C#. You're missing an array declaration after Elements =. It should be something like:

Elements = new ViewModelBase[] { /* ... */ };

Once you've fixed that, formatting might start working correctly. It does for me (but I'm using ReSharper).


I believe you need a default constructor on your TextViewModel and CheckViewModel.

As far as I am aware that's the only way .net can do it. Also, I am assuming those two objects inherit from a base class, and your array is the same type as your base class.

Otherwise the objects created will be of type "var". The compiler throws a fit because the array would technically have to be of the same type "var" as well.

Also, the parameter that you pass to your object's ctors should be initialized in a property just like the properties Label and Watermark of the TextViewModel object, and Style, Default, and Label of the CheckViewModel respectively.

0

精彩评论

暂无评论...
验证码 换一张
取 消