开发者

Vb.net Exception

I have just converted my c# code to vb.net. And it is giving exception.

c# code :

private static short[] muLawToPcmMap;

decoded[2 * i + 1] = (byte)(muLawToPcmMap[data[i]] >> 8);

vb code :

decoded(2 * i + 1) = CByte(muLawToPcmMap(data(i)) >> 8)

Exc开发者_如何学编程eption :

Arithmetic operation resulted in an overflow.

I am in very much trouble. Please help.


Your code is resulting in an overflow for the data type you're working with.

The default behavior of VB.NET is to check arithmetic operations and in C# it is to not check arithmetic operations.

Add a checked statement around your C# code to see it fail also.

checked {
   // do all your work here, any overflow will cause an exception
}

Fix your code to stop overflowing. As my comments below mention, an arithmetic overflow is not something to be ignored necessarily. You're performing operations here that result in likely unexpected results and you should code explicitly for this (by increasing the size of your type or handling the failure).

The absolute last thing you should (IMO) do is under your project properties, Compile tab, Advanced Compiler Settings button, is check the checkbox labeled "Remove integer overflow checks". I personally think it's a bad idea and personally I use checked in C# whenever I do things that will overflow my variables. Fail early, fail often and all.


Check if your translation of

byte[] decoded = new byte[size*2]; 

is

Dim decoded As Byte() = New Byte(size * 2 - 1) {}

or not, as in vb.net, you declare arrays with the index of the last element, not the size itself.

Depending on how you translated decoded, you have to check the rest of the code to adapt it to the version you chose.


It's most likely that the number you are trying to convert is outside the range of Byte or Short

Reference: link text


I notice that your C# code doesn't contain a cast to short, but your VB.NET code does. It's possible the cast is causing a shift of a bit into a bit that's causing an overflow. (Can't really tell without seeing the data.) Remove it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜