Object Initializer Formatting - Confusion
Hi
Firstly we are on VS2008 and R#5
I am trying to make Resharper format object initializers as follows (so that each parameter is on a new line)
var x = new MyObject()
{
param1 = xxx,
param2 = uuu,
param3 = ttt
}
I have meddled with the settings for over an hour now and no matter what I do I always end up with the parameters on one line. e.g.
{ param1 = xxx, param2 = uuu, param3 = ttt }
OR
{
param1 = xxx, param2 = uuu, param3 = ttt
}
Resharper is such a great tool but this is really making me pull my hair out!
Thanks
Please sign in to leave a comment.
Hello Matthew,
Please open ReSharper | Options | Languages | C# | Formatting Style | Line
Breaks and Wrapping and turn off 'Place simple array, object and collection
initializer on a single line' option. Thank you!
Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
Hi Andrey
Thanks for your reply. I tried that yesterday but all I end up with is the parameters on one line e.g.
var x = new MyObject()
{
param1 = 1, param2 = 2, param3 = 3
}
I want them to be split as follows
var x = new MyObject()
{
param1 = 1,
param2 = 2,
param3 = 3
}
I fiddled with the settings and can't figure it out. I have setup resharper to match the coding standards for our business so that all my developers use the same file and therefore we are conisistant across all our solutions - however this is the only item I can't seem to get working how I want.
Best regards
Matthew
Hello Matthew,
Please also make sure that you have 'Wrap long lines' option turned on under
'Line Breaks and Wrapping' section. Thank you!
Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
Hi there,
I also have the same problem, and turning on "Wrap Long Lines" doesn't do what I want it to do...
Here's the original code:
var iddData = new AdminLegalEntityIDD {
AuthFirmName = txtAuthFirmName.Text.Trim(), AFStreet = txtAFStreet.Text.Trim(), AFDistrict = txtAFDistrict.Text.Trim(), AFTown = txtAFTown.Text.Trim(), AFCounty = txtAFCounty.Text.Trim(), AFPostCode = txtAFPostCode.Text.Trim(), AppointedRep = cbAppointedRep.Checked, ProductsRange = cbProductsRange.Checked, ProductsLimited = cbProductsLimited.Checked, ProductsSingle = cbProductsSingle.Checked, ProductsNonInvest1 = txtProductsNonInvest1.Text.Trim(), ProductsNonInvest2 = txtProductsNonInvest2.Text.Trim(), ProductsPlural = ddlProductsPlural.SelectedItem.Text, SingleInsurer_MPPI = cbMPPI.Checked, SingleInsurer_BC = cbBC.Checked, SingleInsurer_LLBC = cbLLBC.Checked, SingleInsurer_STIP = cbSTIP.Checked, FreeText = txtFreeText.Text.Trim(), ServiceAdvice = cbServiceAdvice.Checked, ServiceNonAdvice = cbServiceNonAdvice.Checked, ServiceNonInvest1 = txtServiceNonInvest1.Text.Trim(), ServiceNonInvest2 = txtServiceNonInvest2.Text.Trim(), FeeYes = cbFeeYes.Checked, FeeNo = cbFeeYes.Checked, FeeList = txtFeeList.Text.Trim(), NoFeeList = txtNoFeeList.Text.Trim(), RAFStreet = txtRAFStreet.Text.Trim(), RAFDistrict = txtRAFDistrict.Text.Trim(), RAFTown = txtRAFTown.Text.Trim(), RAFCounty = txtRAFCounty.Text.Trim(), RAFPostCode = txtRAFPostCode.Text.Trim(), RegulatesOmitSection = cbRegulatesOmitSection.Checked, FSARegNo = txtFSARegNo.Text.Trim(), RegulatesPermittedBusiness1 = txtRegulatesPermittedBusiness1.Text.Trim(), OwnershipNotes1 = txtOwnershipNotes1.Text.Trim(), OwnershipNotes2 = txtOwnershipNotes2.Text.Trim(), OwnershipSectionOmit = cbOwnershipSectionOmit.Checked, ComplaintAuthFirmName = txtComplaintAuthFirmName.Text.Trim(), COMStreet = txtCOMStreet.Text.Trim(), COMDistrict = txtCOMDistrict.Text.Trim(), COMTown = txtCOMTown.Text.Trim(), COMCounty = txtCOMCounty.Text.Trim(), COMPostCode = txtCOMPostCode.Text.Trim(), ComplaintTel = txtComplaintTel.Text.Trim(), ComplaintsAlternativeDetails = txtComplaintsAlternativeDetails.Text.Trim(), ComplaintSectionOmit = cbComplaintSectionOmit.Checked, ComplaintCommercialOmit = cbComplaintCommercialOmit.Checked, ProfessionalFirm = cbProfessionalFirm.Checked, FSCSMember = cbFSCSYesNo.SelectedIndex == 0, CompensationSectionOmit = cbCompensationSectionOmit.Checked
};
Don't ask me why the object is badly designed, I'm just working with it ;P
When I turn on "Wrap Long Lines" and reformat the code, I get this:
var iddData = new AdminLegalEntityIDD {
AuthFirmName = txtAuthFirmName.Text.Trim(), AFStreet = txtAFStreet.Text.Trim(),
AFDistrict = txtAFDistrict.Text.Trim(), AFTown = txtAFTown.Text.Trim(), AFCounty = txtAFCounty.Text.Trim(),
AFPostCode = txtAFPostCode.Text.Trim(), AppointedRep = cbAppointedRep.Checked,
ProductsRange = cbProductsRange.Checked, ProductsLimited = cbProductsLimited.Checked,
ProductsSingle = cbProductsSingle.Checked, ProductsNonInvest1 = txtProductsNonInvest1.Text.Trim(),
ProductsNonInvest2 = txtProductsNonInvest2.Text.Trim(), ProductsPlural = ddlProductsPlural.SelectedItem.Text,
SingleInsurer_MPPI = cbMPPI.Checked, SingleInsurer_BC = cbBC.Checked, SingleInsurer_LLBC = cbLLBC.Checked,
SingleInsurer_STIP = cbSTIP.Checked, FreeText = txtFreeText.Text.Trim(), ServiceAdvice = cbServiceAdvice.Checked,
ServiceNonAdvice = cbServiceNonAdvice.Checked, ServiceNonInvest1 = txtServiceNonInvest1.Text.Trim(),
ServiceNonInvest2 = txtServiceNonInvest2.Text.Trim(), FeeYes = cbFeeYes.Checked, FeeNo = cbFeeYes.Checked,
FeeList = txtFeeList.Text.Trim(), NoFeeList = txtNoFeeList.Text.Trim(), RAFStreet = txtRAFStreet.Text.Trim(),
RAFDistrict = txtRAFDistrict.Text.Trim(), RAFTown = txtRAFTown.Text.Trim(), RAFCounty = txtRAFCounty.Text.Trim(),
RAFPostCode = txtRAFPostCode.Text.Trim(), RegulatesOmitSection = cbRegulatesOmitSection.Checked,
FSARegNo = txtFSARegNo.Text.Trim(), RegulatesPermittedBusiness1 = txtRegulatesPermittedBusiness1.Text.Trim(),
OwnershipNotes1 = txtOwnershipNotes1.Text.Trim(), OwnershipNotes2 = txtOwnershipNotes2.Text.Trim(),
OwnershipSectionOmit = cbOwnershipSectionOmit.Checked, ComplaintAuthFirmName = txtComplaintAuthFirmName.Text.Trim(),
COMStreet = txtCOMStreet.Text.Trim(), COMDistrict = txtCOMDistrict.Text.Trim(), COMTown = txtCOMTown.Text.Trim(),
COMCounty = txtCOMCounty.Text.Trim(), COMPostCode = txtCOMPostCode.Text.Trim(),
ComplaintTel = txtComplaintTel.Text.Trim(),
ComplaintsAlternativeDetails = txtComplaintsAlternativeDetails.Text.Trim(),
ComplaintSectionOmit = cbComplaintSectionOmit.Checked, ComplaintCommercialOmit = cbComplaintCommercialOmit.Checked,
ProfessionalFirm = cbProfessionalFirm.Checked, FSCSMember = cbFSCSYesNo.SelectedIndex == 0,
CompensationSectionOmit = cbCompensationSectionOmit.Checked
};
I've attached screenshots of my formatting settings pages (I turned Wrapping off again btw)... can someone help please? It's starting to get annoying and I've tried loads of different options...
I would rather not turn on wrapping anyway, as I don't like it... I want some long lines to remain long, but want my object initializers to look like this:
var x = new MyObject {
Test1 = "test",
Test2 = "test2",
SomeLongerNameThatICantThinkUpOffTheTopOfMyHead = true
};
Attachment(s):
c#other.GIF
c#linebreaks.GIF
c#braces.GIF
c#blanklines.GIF
Spaces screenshots attached...
Attachment(s):
c#spaces2.GIF
c#spaces1.GIF
Same here, I've already made a post about it and got no answer.
Hello Chris,
Try changing 'Wrap object and collection initializer' option under ReSharper
| Options | Languages | C# | Code Style | Line Breaks and Wrapping to 'Chop
always'. Let me know if this helps. Thank you!
Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
Absolutely brilliant, worked perfectly :) I'll confirm properly next time I write a new initializer :)