Select Case ambiguity in VB.Net

by Miha Markič 18. January 2007 11:44

Consider this piece of code:

Dim a As Integer = 0 Select Case a Case 0 Console.WriteLine("a") Case 0 Console.WriteLine("b") End Select Console.ReadLine()

As a C# developer I was pretty sure that this code will never get compiled. Of course I was wrong. I leave to you guessing which line gets executed.

Does this relaxed compilation makes sense to you? You don't even get a warning of a clear error in your Select Case. Imagine you have bigger Select Case with a lot of Case lines. You can overlook ambiguity pretty quickly and your application will behave oddly. If you ask me, this feature is just calling for troubles.

If you wonder - C# won't compile such code ever and this is the only right way of dealing with this issue. You'll get pretty descriptive error on second case label:

Error 1 The label 'case 0:' already occurs in this switch statement ...\Projects\WindowsApplication22\Form3.cs 22 22 WindowsApplication22

Compile time errors are best way to find the errors. Far better than your customers finding them.

Tags:

.net

Comments (6) -

Jens Samson
Jens Samson
1/18/2007 3:00:19 PM #

Although I'm a VB.Net developer, I couldn't agree with you more ...

Reply

Mare
Mare
1/19/2007 12:25:20 AM #

One more c# feature

Reply

Skuta / Greg
Skuta / Greg
1/30/2007 4:47:10 PM #

My philosophic approach would be hovever in favour of VB option. SELECT...CASE does not appear to me as math... exclusive function. Therefore.. leave it up to developer

Reply

Miha Markic
Miha Markic
1/30/2007 5:10:30 PM #

Well, yeah, this is the problem with VB - not being strict. What good does having two possible outcomes out of same input is beyond me - it might just mislead you and it eventually will.

Reply

My name
My name
2/10/2007 9:37:00 AM #

Please tell what gets executed finally

Reply

Miha Markic
Miha Markic
2/10/2007 12:52:31 PM #

Hi there,

I think the first condition met gets executed - first case that mets the condition.

Reply

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Miha Markic

About me
Righthand
 
Microsoft MVP
 
Developer Express' DXSquad
INETA Country Leader for Slovenia
INETA Country Leader for Slovenia

Slovene Developer Users Group Lead
Friends of Red-Gate
LLBLGenPro Partner

Miha currently works as a free lance consultant and software developer specialized in .net area.
He graduated in Computer and information science at the University of Ljubljana, Slovenia. He has accumulated experience in various programming languages such as Java, Visual Basic 3-6 (MCP), Visual C++, Delphi, C# and VB.Net through years.
He has experience in practically all (technical) stages of project development, including planning, framework development, user interface, business processes, as well as testing and documenting. He has worked on big and small projects in Slovenia and abroad (e.g. participated in completing level 3 IS for the Nucor steel plant, Hertford, USA).
Currently he enjoys programming in .net environment using C#. Since 2000 he has been active in Developer Express' DX Squad and has been ECDL trainer and tester. He also gives lectures on conferences and other events in Slovenia.

Month List

Tag cloud

Most comments

Paulius Paulius
1 comments
us United States
Meh Meh
1 comments
us United States
bart dm bart dm
1 comments
nl Netherlands

RecentComments

Comment RSS