473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

"select case" statement optimization in VB and C# problems

Good afternoon. I was just going through my code, analyzing it with FXCop,
and FxCop gave me the following error on this code:

MY CODE:
Select Case termYears
Case 5 : retVal.Append("1")
Case 7 : retVal.Append("2")
Case 10 : retVal.Append("3")
Case 15 : retVal.Append("4")
Case 20 : retVal.Append("5")
Case 30 : retVal.Append("6")
Case Else
Throw New ArgumentException("Term (in years) not recognized!")
End Select

FXCOP's ERROR:
* Operations should not overflow.
Correct the potential overflow in the operation 'settleMonth-10' in (my
function).

So I found this to be QUITE a curious result, of course, since I wasn't
performing a subtraction on the code. I looked into the documentation of the
"Select Case" statement, and I didn't find anything about random overflow
exceptions...

So I read this article:
http://weblogs.asp.net/justin_rogers.../25/95806.aspx
which was quite good. And I ran Reflector on my code and disassembled it.
Sure enough, the code disassembled (and reassembled into C# by reflector) to:

REFLECTOR'S GENERATED CODE:
switch (((short) (termYears - 5)))
{
case 0:
builder1.Append("1");
break;

case 2:
builder1.Append("2");
break;

case 5:
builder1.Append("3");
break;

case 10:
builder1.Append("4");
break;

case 15:
builder1.Append("5");
break;

case 0x19:
builder1.Append("6");
break;
}

OK, now that I've set the scene for my question, I'll ask it. HOW IN THE
WORLD IS THIS NOT A MAJOR DESIGN BUG IN THE .NET COMPILER? There's NO way I,
the developer, can know that my code's going to throw an OverflowException if
my code gets short.MinValue for the termYears parameter. Anyone reading my
original code should be expecting ArgumentException, but instead, they'll get
this mysterious OverflowException? Is this going to be fixed somehow? I
understand that this is a speed optimization, but you CAN'T make
optimizations under the covers which change code semantics!

Thanks a bunch. I'd appreciate a (well thought out, not dismissive) reply
by someone the VB/.NET/C# team as soon as possible. IMO this is potentially
a major issue.

-Dave

Jun 1 '06 #1
2 2939
Dave,
REFLECTOR'S GENERATED CODE:
switch (((short) (termYears - 5))) [...]OK, now that I've set the scene for my question, I'll ask it. HOW IN THE
WORLD IS THIS NOT A MAJOR DESIGN BUG IN THE .NET COMPILER? There's NO way I,
the developer, can know that my code's going to throw an OverflowException if
my code gets short.MinValue for the termYears parameter. Anyone reading my
original code should be expecting ArgumentException, but instead, they'll get
this mysterious OverflowException?


Did you try that? Did you actually get an OverflowException? I always
get the ArgumentException as expected.

If we ignore the Reflector output and look at the IL produced, I see
the following generated by all VB compiler versions I have, after
compiling the code you posted

ldloc.1 // load termYears
ldc.i4.5
sub
switch ( ...

Note that the sub instruction is used, regardless of your
/removeintchecks setting. The sub instruction never throws, it simply
overflows the result. Plus it works on 32 bit arguments or bigger, so
even if termYears is Int16.MinValue, the result will be Int16.MinValue
- 5 stored as a Int32. The switch instruction then interprets this
value as unsigned, and it simply doesn't match any of the cases.

I don't know where Reflector got the truncating cast to short from,
it's not represented in the IL I was able to generate. A more correct
decompiled version would look like this

switch (unchecked(termYears - 5))
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jun 1 '06 #2
Mattias:

You're absolutely right, thanks. Btw, my termYears variable was a "short".
FxCop was leading me astray. I kneel in deference to your mad IL skills. I
guess this one is for the FXCop team as FxCop thinks that it's a checked
operation.

-Dave

"Mattias Sjögren" wrote:
Dave,
REFLECTOR'S GENERATED CODE:
switch (((short) (termYears - 5)))

[...]
OK, now that I've set the scene for my question, I'll ask it. HOW IN THE
WORLD IS THIS NOT A MAJOR DESIGN BUG IN THE .NET COMPILER? There's NO way I,
the developer, can know that my code's going to throw an OverflowException if
my code gets short.MinValue for the termYears parameter. Anyone reading my
original code should be expecting ArgumentException, but instead, they'll get
this mysterious OverflowException?


Did you try that? Did you actually get an OverflowException? I always
get the ArgumentException as expected.

If we ignore the Reflector output and look at the IL produced, I see
the following generated by all VB compiler versions I have, after
compiling the code you posted

ldloc.1 // load termYears
ldc.i4.5
sub
switch ( ...

Note that the sub instruction is used, regardless of your
/removeintchecks setting. The sub instruction never throws, it simply
overflows the result. Plus it works on 32 bit arguments or bigger, so
even if termYears is Int16.MinValue, the result will be Int16.MinValue
- 5 stored as a Int32. The switch instruction then interprets this
value as unsigned, and it simply doesn't match any of the cases.

I don't know where Reflector got the truncating cast to short from,
it's not represented in the IL I was able to generate. A more correct
decompiled version would look like this

switch (unchecked(termYears - 5))
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Jun 2 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: ian justice | last post by:
Before i post actual code, as i need a speedyish reply. Can i first ask if anyone knows off the top of their head, if there is a likely obvious cause to the following problem. For the moment i've...
16
by: lkrubner | last post by:
Are there any benchmarks on how much an extra, unneeded VARCHAR, CHAR, INT, BIGINT, TEXT or MEDIUMTEXT slows down a database call with MySql? PostGre info would also be useful. I'm trying to...
0
by: Michael | last post by:
I have a problem forcing files to download. If I select Save the document is saved with no problems. If I select "Open" the document is empty or I get a "File not found" error from the application...
10
by: serge | last post by:
Using "SELECT * " is a bad practice even when using a VIEW instead of a table? I have some stored procedures that are identical with the difference of one statement in the WHERE clause. If I...
4
by: Lucky | last post by:
hi guys! Currently i'm facing a bit odd situation. i cant modify my code so i have to make changes in SQL Server. the problem is, i've modified one table by adding new column and now i want...
5
by: Henning M | last post by:
Hi all, I having some problems with Access and selecting records between dates.. When I try this in access, it works fine!! "Select * from Bilag Where Mdates Between #1/1/2006# And...
3
by: divya | last post by:
Hi, I have a table tblbwday with 2 fields Name and Birthday.I have written this script for displaying evryday names of the people on that day. <% set objConn...
3
by: sangam56 | last post by:
Hello!I am using following sql statement: SELECT Menu.MenuID,Menu.TextUrl FROM Menu WHERE Menu.MenuID= (SELECT Permissions.MenuID FROM Permissions WHERE Permissions.RoleID=(SELECT Roles.RoleID...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.