Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more: , +
After an auto conversion task of one VB project to C#, tool doing this task dropped all Carriage Returns (\r\n) from my code. Now I have very long and diffucult to read code lines.

Either one by one enter on the desired location of code line to get a break in the line or must use REGEX (I mean Replace Tool of VS with wild chars). REGEX does this job and breaks line where you wanted successfully. But how I calculate the starting location of new line. I do not want new line starting with a nonsense distance to left margin. Let me give example:

C#
string slsqt = "SELECT * FROM SalesTable WHERE Year(SalesDate) > 2001";


Press ENTER on 'Year' in Visual Studio you will get just two lines like these:

string slsqt = "SELECT * FROM SalesTable WHERE
Year(SalesDate) > 2001";

Nice, it started one TAB ahead from left margin. Well how to do this with REGEX Replace using \r\n ?

What I have tried:

Either one by one enter on the desired location of code line to get a break in the line
Posted
Updated 19-Mar-21 9:04am

That won't work as "valid C# code" - it will be treated as two lines of code:
C#
string slsqt = "SELECT * FROM SalesTable WHERE
And
C#
Year(SalesDate) > 2001";

The lack of a terminating double quote on the first line will give you an error, and you will get further errors for a missing function "Year", a missing variable "SalesDate", and a "what is that string doing in there?" for the double quotes.
You can do that if you turn off string escaping:
C#
string slsqt = @"SELECT * FROM SalesTable WHERE
Year(SalesDate) > 2001";
will be treated as a single line, but ... the whitespace will then be treated as part of the string literal which makes the two pieces of code functionally different.
That's not the idea at all with layout changes! :laugh:

And ... you can't really do this automatically with a blunt instrument like a Regex: the amount of spacing you need depends on the indentation of the word string as well as the desired offset to "match up" the two parts of the literal.

If you want to faff about with layouts, then let VS do it for you: "Tools ... Options ... Text Editor ... C# ... Code style ... Formatting" gives you a wealth of options you can try.
 
Share this answer
 
Comments
Cemal Sener 15-Aug-20 5:03am    
to make it understandable " + " not added to above code part...

"the amount of spacing you need depends on the indentation of the word string as well as the desired offset to "match up" the two parts of the literal." sounds good but how can be achieved with REGEX ?
OriginalGriff 15-Aug-20 5:23am    
Pretty much, you can't - Rexeges are text processors, and your trying to get it to do maths! It may be possible to do it to a certain extent, but it'll be full of special cases and about as maintainable as a first year students VB code ...

Seriously, use VS to layout your code (because if you don't then it'll probably "mess it up" again later when you edit it), or write a "code beautifier" that works according to your specific rules in C# or similar, and use that.
But VS will probably still mess that up when you edit things ... :laugh:
Cemal Sener 15-Aug-20 9:23am    
sharing impossibility of task as you state, but still trying some chance to get, thanks...
What did you use to convert the VB code to C# ?

Did you format the VB code using Visual Studio, or any other tool, before conversion ?

JetBrains' free 'DotPeek decompiler can view any assembly as C#: I am not sure that can help you with your specific issues: [^]
 
Share this answer
 
Comments
Cemal Sener 15-Aug-20 9:25am    
it was NUGET a package I do not remember, since task finished 1 year ago and another one year to debug C# (25 project), now have time to get visual side code, thanks...
I'm surprised you haven't run your code through something like resharper, or stylecop or there must be other 'lint' tools for C# that would help you reformat the code to 'some acceptable standard'
 
Share this answer
 
Comments
Cemal Sener 15-Aug-20 5:00am    
problem is, I want to break code line where I see it is logical, resharper or others can not not feel it...

SELECT .. FROM ... WHERE had to be split after WHERE (which can be done easily with REGEX but you have add some SPACE to indent it on the next line to the start location you want), tools does not know this logic naturally...
BillWoodruff 15-Aug-20 8:14am    
Which of these tools have you actually used ?
Because verbatim strings preserve new line characters as part of the string text, they can be used to initialize multiline strings.

Strings - C# Programming Guide | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900