Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I keep my properties inline with attributes as well?

I would like to find a way to tell visual studio to keep my properties in my classes inline with the attributes. An example.

[DataMember] int ID { get; set; }
[DataMember] public int DivisionID { get; set; }
[DataMember] public string Name { get; set; }
[DataMember] public int MyID { get; set; }
[DataMember] public string ContactName { get; set; }

But if I were to modify the class manually and hit save it auto adjusts the file to look like this.

[DataMember] 
int ID { get; set; }
[DataMember] 
public int DivisionID { get; set; }
[DataMember] 
public string Name { get; set; }
[DataMember] 
public int MyID { get; set; }
[DataMember] 
public string ContactName { get; set; }

Edit

Just so everyone knows. This isn't a best practice question. I am just trying to learn if the editor has this capability. These code files are being auto generated by a code generator we had just finished. It is more of an annoyance that keeps happening and we would like to stop it from doing so. I know there are several options for modifying the editor but didn't know if anyone knew of a good workaround to this problem or if it existed.

I really appreciate anyone's insight into our question.

like image 363
meanbunny Avatar asked Oct 21 '22 22:10

meanbunny


2 Answers

Sorry, the answer is that "Visual Studio" doesn't have that option. It isn't the ideal answer, but sometimes the answer is that it can't be done without new code.

If you go here . . .

Tools | Options | Text Editor | C# | Formatting

. . . then yes, you can turn off autoformating completely, which to me is not a solution.

However, if you go to . . .

Tools | Options | Text Editor | C# | Formatting | New Lines

. . . there is not option or setting here for new lines after an Attributes. This option or setting needs to exist.

So this feature would have to be added to Visual Studio. I don't know if it can be added by anybody but Microsoft, so the only solution is to submit and enhancement request.

like image 84
Rhyous Avatar answered Oct 31 '22 19:10

Rhyous


I think you can solve this in the following way:

Tools -> Options -> Text Editor -> C# -> Formatting

Uncheck all the relevant ones (e.g. format completed block on })

Obviously this will affect all the automatic block formatting when you write code.

Another solution is moving to VS2012, which in my opinion doesn't do this (still does it on pasting but this is less prominent).

like image 42
Alexey Avatar answered Oct 31 '22 17:10

Alexey