Can you spot the problem (VBx/VB.NET)

by Miha Markič 18. October 2006 10:16

WARNING: VB code!

Here is a very popular problem that many VB developers fail to see. Furthermore, it is hard to find once in the code. Can you spot it?

 

imports Microsoft.VisualBasic imports System public module MyModule sub Main dim a as integer = 0 dim b as integer = CInt(iif(a=0, 100, SomeCalc(a))) end sub private function SomeCalc(a as integer) as integer if a = 0 then throw new ArgumentNullException("a") else return CInt(100 / a) end if end function end module

Tags:

.net

Comments (7) -

Giuseppe
Giuseppe
10/18/2006 3:05:43 PM #

oh my lovely vb Wink

iff runs always the true and the false part of expression?

Reply

Miha Markic
Miha Markic
10/18/2006 3:11:04 PM #

And the grand prize (fame) goes to Giuseppe Smile, indeed, IIF *always* runs both parts reagardless of condition.
This is one of the nastiest bug in VB since many VBers fails to realize this fact. And later on it is hard to spot...

Reply

Giuseppe
Giuseppe
10/18/2006 3:41:30 PM #

i have another nasty "bug" in VB.NET

Dim test As Integer
test = Nothing

ha ??? int32 is a reference type? nice! who needs Nullable Types? lol
the vb compiler translate this line of code to test = 0

i don't understand why the vb.net compiler allows that.

Reply

Frans Bouma
Frans Bouma
10/18/2006 3:45:23 PM #

Both parts!!??? Whoa... Smile So it doesn't work at all?

Reply

Miha Markic
Miha Markic
10/18/2006 4:05:24 PM #

Giuseppe: I don't understand the rationale either. Seems like a bug to me. "Option Strict On" should issue an error but it doesn't. And yes, the implicit conversions are another menace to developers.

Frans: Well, you could look it from this perspective: since VB runs two statements and C# runs only one (using ternary operator ?: ) it makes VB a more powerfull language :-P

Reply

Matej
Matej
10/18/2006 10:24:20 PM #

SomeCalc(a) has to be calculated before is passed to Iif function (or am I wrong?) - so this is normal operation.

Reply

Miha Markic
Miha Markic
10/18/2006 11:29:20 PM #

You are correct. But trust me, many VBers think it works like ?: operator.

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

Dan Dan
4 comments
ca Canada
Thomas Thomas
3 comments
de Germany
Sebastian Sebastian
1 comments
ca Canada

RecentComments

Comment RSS