<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Spot the Bug! – Much ado about Nothing… Part 2! (Jonathan Aneja)</title><link>http://blogs.msdn.com/b/vbteam/archive/2009/11/05/spot-the-bug-much-ado-about-nothing-part-2-jonathan-aneja.aspx</link><description>In yesterday’s post we saw that the use of the wrong comparison operator with Nothing in an If block can lead to surprising results. Let’s look at a slightly different case today: 
 
 Dim x As Integer = Nothing 
 
 If x = Nothing Then 
 MsgBox( </description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Spot the Bug! – Much ado about Nothing… Part 2! (Jonathan Aneja)</title><link>http://blogs.msdn.com/b/vbteam/archive/2009/11/05/spot-the-bug-much-ado-about-nothing-part-2-jonathan-aneja.aspx#9945635</link><pubDate>Fri, 08 Jan 2010 09:58:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9945635</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;I have to be anonymous here in case it's a breach of NDA, but &amp;quot;= Nothing&amp;quot; came up in a recent MCP exam as part of the code sample. &amp;nbsp;I also think it was a bug, but I'm not sure as obviously I wasn't allowed to take a copy out of the room with me. &amp;nbsp;I believe it's a case of mistranslation from C#.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9945635" width="1" height="1"&gt;</description></item><item><title>re: Spot the Bug! – Much ado about Nothing… Part 2! (Jonathan Aneja)</title><link>http://blogs.msdn.com/b/vbteam/archive/2009/11/05/spot-the-bug-much-ado-about-nothing-part-2-jonathan-aneja.aspx#9927990</link><pubDate>Tue, 24 Nov 2009 14:49:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9927990</guid><dc:creator>yelinna</dc:creator><description>&lt;p&gt;I prefer not to use &amp;quot;nulls&amp;quot; or &amp;quot;Nothings&amp;quot; for numeric variables (I prefer 0 or -1 for detecting errors or invalid values). You're right, &amp;quot;Nothing&amp;quot; is confusing.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9927990" width="1" height="1"&gt;</description></item><item><title>re: Spot the Bug! – Much ado about Nothing… Part 2! (Jonathan Aneja)</title><link>http://blogs.msdn.com/b/vbteam/archive/2009/11/05/spot-the-bug-much-ado-about-nothing-part-2-jonathan-aneja.aspx#9926618</link><pubDate>Sat, 21 Nov 2009 02:42:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9926618</guid><dc:creator>MatthiasBroschk</dc:creator><description>&lt;p&gt;I really doubt that I will ever use something like this, but it can't hurt to know how it works.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9926618" width="1" height="1"&gt;</description></item><item><title>re: Spot the Bug! – Much ado about Nothing… Part 2! (Jonathan Aneja)</title><link>http://blogs.msdn.com/b/vbteam/archive/2009/11/05/spot-the-bug-much-ado-about-nothing-part-2-jonathan-aneja.aspx#9919842</link><pubDate>Mon, 09 Nov 2009 22:41:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9919842</guid><dc:creator>Anthony D. Green</dc:creator><description>&lt;p&gt;Steve,&lt;/p&gt;
&lt;p&gt;I'm not sure I follow. The example is meant to show that for comparison puposes over non-nullable Integers Nothing and 0 are equivalent. Could you explain how you are noticing this difference in your code? Please be detailed.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9919842" width="1" height="1"&gt;</description></item><item><title>re: Spot the Bug! – Much ado about Nothing… Part 2! (Jonathan Aneja)</title><link>http://blogs.msdn.com/b/vbteam/archive/2009/11/05/spot-the-bug-much-ado-about-nothing-part-2-jonathan-aneja.aspx#9919278</link><pubDate>Sun, 08 Nov 2009 19:18:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9919278</guid><dc:creator>Steve</dc:creator><description>&lt;p&gt;Anthony, you miss understod what I was trying to say.&lt;/p&gt;
&lt;p&gt;AS the example shows if you do dim x as int = nothing then x=0 if you don't intialize the variable dim x as int then X&amp;lt;&amp;gt;0 it does equal nothing. &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9919278" width="1" height="1"&gt;</description></item><item><title>re: Spot the Bug! – Much ado about Nothing… Part 2! (Jonathan Aneja)</title><link>http://blogs.msdn.com/b/vbteam/archive/2009/11/05/spot-the-bug-much-ado-about-nothing-part-2-jonathan-aneja.aspx#9919156</link><pubDate>Sun, 08 Nov 2009 01:14:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9919156</guid><dc:creator>Jonathan Aneja</dc:creator><description>&lt;p&gt;Re: &amp;quot;The fact that nothing can be legally assigned to a value type is a serious problem&amp;quot; &lt;/p&gt;
&lt;p&gt;and &lt;/p&gt;
&lt;p&gt;&amp;quot;To avoid confusion, value types should not even be able to be used with the nothing keyword.&amp;quot;&lt;/p&gt;
&lt;p&gt;The logical implication of both those statements is that &amp;quot;it should be illegal to initialize a value type to its default value&amp;quot;, which I'm sure we'd all agree is not a good idea :)&lt;/p&gt;
&lt;p&gt;Consider the following code in C#:&lt;/p&gt;
&lt;p&gt;int x = default(int);&lt;/p&gt;
&lt;p&gt;IntPtr y = default(IntPtr);&lt;/p&gt;
&lt;p&gt;Process p = default(Process);&lt;/p&gt;
&lt;p&gt;The following VB is *exactly* equivalent to those lines:&lt;/p&gt;
&lt;p&gt;Dim x as Integer = Nothing&lt;/p&gt;
&lt;p&gt;Dim y As IntPtr = Nothing&lt;/p&gt;
&lt;p&gt;Dim p As Process = Nothing&lt;/p&gt;
&lt;p&gt;Nothing just means &amp;quot;default value for type&amp;quot;, not necessarily null.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9919156" width="1" height="1"&gt;</description></item><item><title>re: Spot the Bug! – Much ado about Nothing… Part 2! (Jonathan Aneja)</title><link>http://blogs.msdn.com/b/vbteam/archive/2009/11/05/spot-the-bug-much-ado-about-nothing-part-2-jonathan-aneja.aspx#9918942</link><pubDate>Sat, 07 Nov 2009 01:33:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9918942</guid><dc:creator>Anthony D. Green</dc:creator><description>&lt;p&gt;Jason, Novlok;&lt;/p&gt;
&lt;p&gt;I don't know if such a restriction is necessary.&lt;/p&gt;
&lt;p&gt;Whether equating Nothing with defaultness is logically valid I might argue is contextual. &lt;/p&gt;
&lt;p&gt;From a language (English) perspective would we not say that a free item costs Nothing (it doesn't cost anything) or would you say it cost $0.00?&lt;/p&gt;
&lt;p&gt;In the case of strings &amp;quot;&amp;quot; and Nothing compare as equal to the VB comparison operator and Len(&amp;quot;&amp;quot;) = Len(Nothing), And Len(Nothing) = Len(&amp;lt;empty array&amp;gt;).&lt;/p&gt;
&lt;p&gt;Furthermore System.Char is a value type (structure) and its default value is ChrW(0) - commonly called the null character. The only way to represent this as a literal in Visual Basic (other than the above function) is to assign it Nothing. In this context assigning Nothing to a value type sets it to the &amp;quot;null&amp;quot; character so it makes sense, no?&lt;/p&gt;
&lt;p&gt;Conceptually a glass doesn't contain 0 liters of water - it's empty - there's Nothing in the glass.&lt;/p&gt;
&lt;p&gt;So from another angle Emptiness, Nullness, and Nothingness are sufficiently synonymous that I wouldn't say the usage is inherently unsensible or &amp;quot;not good&amp;quot; though I would generally avoid this usage when a more meaningful default value (or literal, such as 0) can be used instead.&lt;/p&gt;
&lt;p&gt;-------------&lt;/p&gt;
&lt;p&gt;Steve,&lt;/p&gt;
&lt;p&gt;What's strange about a variable taking on its default value unless otherwise assigned? That's the entire meaning of a *default* value. What's strange is that other languages make you initialize redundantly, no?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9918942" width="1" height="1"&gt;</description></item><item><title>re: Spot the Bug! – Much ado about Nothing… Part 2! (Jonathan Aneja)</title><link>http://blogs.msdn.com/b/vbteam/archive/2009/11/05/spot-the-bug-much-ado-about-nothing-part-2-jonathan-aneja.aspx#9918939</link><pubDate>Sat, 07 Nov 2009 01:22:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9918939</guid><dc:creator>Steve</dc:creator><description>&lt;p&gt;I always thought it strange that VB.net does not acctually initialize value types until they are assigned. For example in the above example if you do not assign the int to nothing then it does treat the int as nothing. &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9918939" width="1" height="1"&gt;</description></item><item><title>re: Spot the Bug! – Much ado about Nothing… Part 2! (Jonathan Aneja)</title><link>http://blogs.msdn.com/b/vbteam/archive/2009/11/05/spot-the-bug-much-ado-about-nothing-part-2-jonathan-aneja.aspx#9918814</link><pubDate>Fri, 06 Nov 2009 21:09:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9918814</guid><dc:creator>Nivlok</dc:creator><description>&lt;p&gt;I agree with jason. &amp;nbsp;Having a value type evalute to nothing is not good. &amp;nbsp;It is not a dynamic variable that just gets created on the heap. I gets created on the stack and will always evaluate to something even if that something for and int is the number 0. &amp;nbsp;To avoid confusion, value types should not even be able to be used with the nothing keyword.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9918814" width="1" height="1"&gt;</description></item><item><title>re: Spot the Bug! – Much ado about Nothing… Part 2! (Jonathan Aneja)</title><link>http://blogs.msdn.com/b/vbteam/archive/2009/11/05/spot-the-bug-much-ado-about-nothing-part-2-jonathan-aneja.aspx#9918767</link><pubDate>Fri, 06 Nov 2009 19:45:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9918767</guid><dc:creator>Jason Lind</dc:creator><description>&lt;p&gt;The fact that nothing can be legally assigned to a value type is a serious problem.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9918767" width="1" height="1"&gt;</description></item></channel></rss>