Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As an example of the difference between expression and statement in VB, in VB the following generates a compiler error since <code>count += 1</code> increments <code>count</code> by 1, but the whole expression <code>count += 1</code> does not return a result, so it can't be used as a parameter.</p> <pre class="lang-vb prettyprint-override"><code>Dim count As Integer = 0 Console.WriteLine(count += 1) ' compiler error </code></pre> <p>You have to do this instead</p> <pre><code>Dim count As Integer = 0 count += 1 Console.Writeline(count) </code></pre> <p>Of course same applies to using the <code>+=</code> operator on a String.</p> <h3>What does "In VB, a statement cannot just be an expression" mean?</h3> <ul> <li>The VB compiler requires results to be consumed in some assignment or other operation.</li> <li>Because of this an assignment operation in VB does <strong>not</strong> produce a result. If it did the VB compiler would not allow it to stand alone as a statement (the compiler requires results be consumed).</li> <li>Thus assignments in VB can be used as statements, but not as expressions. That is you cannot use an assignment statement as a parameter to a method, or as an intermediate result.</li> <li>In C# an assignment operation <strong>does</strong> produce a value. Thus in order for assignments to stand alone as statements, the compiler does <strong>not</strong> require all results to be consumed.</li> <li>The corollary in C# is that any other operation that produces a result can stand alone as a statement. <code>2 + 2</code> for instance produces the result <code>4</code> and can stand alone as a statement, whereas in VB it can't.</li> </ul> <h3>Simplified answer to "Why are pre and post increment operators not available in VB?"</h3> <p><code>count++</code> says, <strong>first</strong> return the value of <code>count</code>, <strong>then</strong> increment <code>count</code> (and do not return the value of the assignment to <code>count</code>).<br> In this case the incremented value is not used (the value before incrementing is used). As mentioned before, the VB compiler requires you use or assign values of operations.</p> <p><code>++count</code> says, <strong>first</strong> increment <code>count</code>, <strong>then</strong> return the value of the assignment to <code>count</code>.<br> In this case, the value of assigning +1 to <code>count</code> is returned as the value of the expression. As mentioned before, assignments in VB do not produce a result.<br> Thus there would be some serious pain implementing these operators in VB.</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. 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.
    3. VO
      singulars
      1. This table or related slice is empty.
    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