3

Got a problem with a code conversion from C# to VB.Net.

var x = 5783615;
var y = 56811584;
var t = x * y;

x, y and t are integers. In c# 't' will be -1553649728. In VB.Net, I will get an integer overflow exception.

Any idea how to fix it?

Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
user1717864
  • 33
  • 1
  • 3

3 Answers3

6

C#, by default, doesn't check for overflows, but VB does (by default).

You can setup your VB Project to not check for integer overflows in the Advanced Compile Options in the Compile tab. This will cause that specific project to stop raising OverflowException in cases like this.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 5
    I'm not sure whether that counts as a "fix". :) – Bazzz Oct 03 '12 at 17:25
  • But surely better to get actual value unless the overflow is a required value. – Peter Smith Oct 03 '12 at 17:26
  • It depends - this makes the VB code behave the same as the C# code, and since it's a port, *if you assume the C# code is correct*, the ported code would be correct in this case. While that may be an incorrect assumption, this will cause the VB project to behave the same as the C# project where things are starting. Maybe good, maybe bad, but that's something I'd fix in unit tests... – Reed Copsey Oct 03 '12 at 17:27
  • @Bazzz This was a port of an existing project, which obviously overflows - so perhaps the overflow is expected and handled properly. – Reed Copsey Oct 03 '12 at 17:28
  • @Bazzz But this is not good practice - values should be checked. Relying on exceptions shoould be a last resort. Surely good practice should be promoted and if this is a port then it is a good opportunity to clean this up :-) – Peter Smith Oct 03 '12 at 17:38
  • @PeterSmith It really depends on what this is being used for, though. There are times when unchecked integer math is perfectly acceptable (ie: implementing GetHashCode() is a good example). That's one advantage in C#, IMO - you can have checked/unchecked scopes within an application without resorting to specifying the entire project must be checked or unchecked. – Reed Copsey Oct 03 '12 at 17:41
4

You can get the same value as C# does ignoring overflow by calculating the product in 64 bits and then using only 32 bits of the result:

Dim x As Integer = 5783615
Dim y As Integer = 56811584
Dim tmp As Int64 = Convert.ToInt64(x) * Convert.ToInt64(y)

Dim bb = BitConverter.GetBytes(tmp)
Dim t = BitConverter.ToInt32(bb, 0)
' t is now -1553649728
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • Thanks. I don't want to completely disable the overflow checks, but want to perform an unchecked conversion in specific places. With your solution this is possible. – Dennis Kassel Jul 20 '15 at 10:31
  • @GinoBambino I have since found that there is a better way: [Are there utility methods for performing unsafe arithmetic in VB.NET?](http://stackoverflow.com/a/31438176/1115360) – Andrew Morton Jul 20 '15 at 10:40
  • Yes. It should also perform better because it requires one cast less. Thank you. – Dennis Kassel Jul 20 '15 at 10:45
0

You need explicit type definition to avoid this as the definition will be implicitly taken from the values. In vb.net try:

dim x as int = 5783615
dim y as int = 56811584
dim t as int = x * y

There may be other ways of doing this but this should be a start. In C# you might also try int or int32 or int64.

Good luck.

Peter Smith
  • 5,528
  • 8
  • 51
  • 77