CSharpToVB icon indicating copy to clipboard operation
CSharpToVB copied to clipboard

About ++ or -- operator

Open Whismeril opened this issue 5 years ago • 3 comments

This code

int i = 0;
//++ or -- onl
i++;
++i;
i--;
--i;

//++ or -- in instruction
DoSomethingWithI(i++);

DoSomethingWithI(++i);

DoSomethingWithI(i--);

DoSomethingWithI(--i);

is converted like this

Private i As Integer = 0
'++ or -- only
i += 1 'perfect
i += 1 'perfect
i -= 1 'perfect
i -= 1 'perfect

'++ or -- in instruction
DoSomethingWithI(Math.Min(Interlocked.Increment(i), i - 1))'makes questions

DoSomethingWithI(Interlocked.Increment(i))

DoSomethingWithI(Math.Max(Interlocked.Decrement(i), i + 1))

DoSomethingWithI(Interlocked.Decrement(i))
  1. Interlocked.Increment(i) should work if System.Threading is imported, conversion doesn't notify that.

  2. In

Math.Min(Interlocked.Increment(i), i - 1)

why first increment and then calcul i - 1 ?

this

Math.Min(i, Interlocked.Increment(i))

does the same with one subtraction less

Math.Min(Interlocked.Increment(i), i - 1)

could be obscure for beginner, convert in 2 lines mays be easier for them

DoSomethingWithI(i++);

goes

DoSomethingWithI(i)
i += 1

and

DoSomethingWithI(++i);

goes

i += 1
DoSomethingWithI(i)

Whismeril avatar Feb 16 '21 18:02 Whismeril

@Whismeril Multiline conversions from single line is complex for translator especially if it is in a loop. In SDK style VB projects System is always included. I could use Math.Min(i, Interlocked.Increment(i)), and I might do some testing with it but I also do decrements. It you want to look at the code and do a PR I will review.

paul1956 avatar Feb 16 '21 22:02 paul1956

@Whismeril think about DoSomethingWithI(i++, i--,..); There are way too many places with multiple statements don't work and may not even be possible.

paul1956 avatar Feb 17 '21 02:02 paul1956

I understand the difficulty to translate one line into two, especially for a loop. And (i++, i--,..); is an unstoppable argument, I didn't thought about it.

I only code in VB.Net in order to try helping beginners in forums. I used to says them to search in C# too and convert code. So when I’ve found you project, couple of days ago, I wanted the test it. It’s very good, and your reactivity with my issues is to your credit.

But when compiling Visual Studio returns « import missing import » to the line DoSomethingWithI(Math.Min(Interlocked.Increment(i), i - 1)) my first reaction wasn’t looking for the messing assembly, but wondering was is this strange way of coding. So I suppose beginner could be lost by compiling error in this strange line. If converter could add Import System.Threading or at least convert like this DoSomethingWithI(Math.Min(System.Threading.Interlocked.Increment(i), i - 1)) no error occurs and beginner could have reflexion on a working line, if he see it.

I tested this case in few online converters, they do the same conversion

Whismeril avatar Feb 17 '21 07:02 Whismeril