CodeConverter icon indicating copy to clipboard operation
CodeConverter copied to clipboard

VB -> C#: Brackets needed around ternary expression

Open Nward42 opened this issue 1 year ago • 3 comments

VB.Net input code

dim x as integer
x = if(true, 1, 2) + 3

Erroneous output

int x;
x = true ? 1 : 2 + 3;

Expected output

int x;
x = (true ? 1 : 2) + 3;

Details

A simplified example of a gotcha hit during a recent VB->C# project conversion using version 9.2.7.0. Simple enough to fix once found. Otherwise, the convertor seems to have worked perfectly. Cheers

Nward42 avatar Mar 25 '25 14:03 Nward42

Thanks for the bug report and feedback on general success!

GrahamTheCoder avatar Apr 03 '25 12:04 GrahamTheCoder

In the online converter, the case you gave seems to work correctly. For ease of repro, do you have an example that fails when pasted into: https://icsharpcode.github.io/CodeConverter/

GrahamTheCoder avatar Apr 03 '25 12:04 GrahamTheCoder

I simplified the example too much. The below yields the issue when pasted into the online converter.

dim i as integer = 0
dim a(If(i = 1, 2, 3)) as string

Converts to:

int i = 0;
var a = new string[i == 1 ? 2 : 3 + 1];

Instead of:

int i = 0;
var a = new string[(i == 1 ? 2 : 3) + 1];

So it looks like the problem is in the addition of 1 for C# array extents.

Nward42 avatar Apr 03 '25 23:04 Nward42

@Nward42 : I believe my PR will fix this issue.

jrmoreno1 avatar Jul 27 '25 01:07 jrmoreno1