Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Suppose you don't initialize your <code>String</code> variable. And there goes some processing. In the end you expect get a definitive value, which you return via a <code>Return</code> statement. If, for some reason, you forgot to account for a possible execution path, VS will warn you about possible null-reference exception. Then you may think to yourself: "oh yeah, how did I forget that?"</p> <p>For example, you have a <code>Function</code> that converts a <code>Integer</code> into a word (limited practical use, so consider it just for demo purposes):</p> <pre><code>Function ConvertIntToWord(a As Integer) As String Dim retString As String If a = 0 retString = "Zero " ElseIf a = 1 retString = "One " End If Return retString End Function </code></pre> <p>In the above lines, you were unlucky to forget about the <code>Else</code> path. Then suppose you want to run further processing on a resulting string:</p> <pre><code>ConvertIntToWord(5).TrimEnd </code></pre> <p>With 5 passed as an argument, you will get an exception at this point. If you instead listened to VS, you would have noticed a warning - underlined <code>retString</code> in <code>Return retString</code> line. So you could fix this problem without running your code.</p> <p>Now suppose you initialize <code>Dim retString As String = ""</code>. You will no longer see the warning, and have to debug your program to solve this issue. Depending on the complexity of your program, it may take much more effort.</p> <p>Bottom line - it is always better to spot problems in compile time, rather than run-time.</p> <p><strong>EDIT:</strong> Forgetting to initialize a variable can be intentional, for example why using <a href="http://msdn.microsoft.com/en-us/library/bb347013.aspx" rel="nofollow">TryGetValue</a>. For this method, value <code>is passed uninitialized</code>, according to documentation. However, VS does not know about this, so it will continue to show a warning, until you explicitly assign it to <code>Nothing</code>:</p> <pre><code>Dim retString As String = Nothing </code></pre> <p>So this is a way to tell VS that yes, you know it is uninitialized, but for this particular scenario, it is intentional.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload