paul1956 / CSharpToVB

New version of CSharpToVB converter
MIT License
25 stars 9 forks source link

OverFlow Conversions #78

Closed VBAndCs closed 3 years ago

VBAndCs commented 3 years ago

You convert this C# code int x = (int)Now.Ticks; To: Dim x as Integer = CInt(Fix(Now.Tics)) And this will cause overflow because C# circulate the overflow while vb not. So,you need to check if the conversion is from a wider type to a narrower type, and hence you can use something like this: Dim x as Integer = Now.Ticks Mod Integer.MaxValue As this will circulate the overflow between 0 and MAxValue -1 which is fine for my case. Or you need to do a better solution to maintain the same C# behavior.

paul1956 commented 3 years ago

Fixed in converter 5.0.1.20. I was special casing this and I changed it to use your solution.

VBAndCs commented 3 years ago

My solution will not give the same C# results with signed numeric types. It does't matter in my case because I deal with randoms. in this sample, x will be -128

  var x = sbyte.MaxValue;
  x += 1;

This value comes from: x - sbyte.MinValue - 1 + sbyte.MinValue

paul1956 commented 3 years ago

@VBAndCs I have a solution but it is long but it works with everything I have tried. Dim aB As Object = 5 Dim Ab1 As Integer = CInt(CLng(Fix(aB)) Mod Integer.MaxValue)