473,545 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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 ArgumentExcepti on("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 OverflowExcepti on if
my code gets short.MinValue for the termYears parameter. Anyone reading my
original code should be expecting ArgumentExcepti on, but instead, they'll get
this mysterious OverflowExcepti on? 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 2951
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 OverflowExcepti on if
my code gets short.MinValue for the termYears parameter. Anyone reading my
original code should be expecting ArgumentExcepti on, but instead, they'll get
this mysterious OverflowExcepti on?


Did you try that? Did you actually get an OverflowExcepti on? I always
get the ArgumentExcepti on 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(term Years - 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 OverflowExcepti on if
my code gets short.MinValue for the termYears parameter. Anyone reading my
original code should be expecting ArgumentExcepti on, but instead, they'll get
this mysterious OverflowExcepti on?


Did you try that? Did you actually get an OverflowExcepti on? I always
get the ArgumentExcepti on 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(term Years - 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
5649
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 reduced my form request to a simple text string entry, instead of my desired optional parameters. As i have been stuck with a single unfathomable...
16
2471
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 explain to some friends the utility of making database calls return only needed data. As an example of what I'm talking about, suppose we had a...
0
1913
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 opening the file... Code: function UTIL_ForceDownloadFile(strFileName, strFileSaveName) Response.Buffer = True Dim strFileSize Const...
10
5591
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 create a single View and specify also in this View the WHERE clause that is common in these stored procedures, I will have the new stored procecures...
4
6832
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 only those row which has not null value in that column whenever the select statement gets fire. i mean in every select statement only those rows should...
5
2546
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 #31/1/2006#" But when I try it from my vb.net app, I get ALL the records in the tabel?? What goes wrong? I haven't been able to find any info on the net,...
3
3078
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 =server.createobject("ADODB.connection") objConn.open "DSN=Photo" Dim sqlSELsite,ObjRSSel sqlSELsite = "SELECT Name FROM tblbwday WHERE B'day ="& date() &" " '
3
1716
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 FROM Roles WHERE Roles.RoleName=@inputRoleName)) I am using three tables: Roles, Permissions, and Menu. The error reads like: use of =,<,> etc cannot...
0
7479
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7411
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7773
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5987
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3468
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.