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?

1
  • Take a look at explicit type definitions. Var is implicit. Commented Oct 3, 2012 at 17:26

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I'm not sure whether that counts as a "fix". :)
But surely better to get actual value unless the overflow is a required value.
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...
@Bazzz This was a port of an existing project, which obviously overflows - so perhaps the overflow is expected and handled properly.
@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 :-)
|
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

3 Comments

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.
@GinoBambino I have since found that there is a better way: Are there utility methods for performing unsafe arithmetic in VB.NET?
Yes. It should also perform better because it requires one cast less. Thank you.
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.

1 Comment

That's not valid VB syntax. Specifically, "int" is not a type in VB. I suspect you meant Integer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.