Hello,

Thanks for the explanation. It is clear to me.

Jan Bodnar


On Aug 26, 9:06 am, Cerebrus <[email protected]> wrote:
> Hi Jan,
>
> Multiple assignment does not work like this in VB (as opposed to C#).
> Your variables are value types and so have already been assigned
> default values (zero in this case) upon declaration (outside the Main
> method). Your statement is seen by the compiler like this :
>
> a = (b = (c = (d = 0)))
>
> This can be broken down into the following three boolean expressions
> (1, 2, 3) and the single assignment expression (4):
>
> 1. d = 0 ?
> True
> (Is d = 0? Yes. No reassignment.)
>
> 2, c = (d = 0) ?
> False
> (Is c = True ? No, because CBool(c) = False, which is not equal to
> True (from previous step). No reassignment.)
>
> 3. b = (c = (d = 0)) ?
> True
> (Is b = False ? Yes, because CBool(b) = False, which is equivalent to
> the result of the previous step. No reassignment.)
>
> 4. a = (b = (c = (d = 0)))
> (Set a = True, the result of the previous step. a is reassigned a
> value of True.)
>
> Now, keep in mind that your variables a, b, c and d are declared as
> bytes, not booleans. Therefore, the variable a will be cast (CByte) to
> a byte type. Booleans are converted to Bytes as follows:
>
> CByte(True) => 255
>
> CByte(False) => 0
>
> Since the variable a, now holds a True value, it is converted to byte
> as 255. The other three variables are not reassigned and remain zero.
>
> This might be a bit complicated but I tried my best to explain the
> process. Let me know if any doubts remain.
>
> On Aug 26, 1:22 am, vronskij <[email protected]> wrote:
>
> > Hi,
>
> > I have the following code snippet.
>
> > Module Module1
>
> >     Dim a, b, c, d As Byte
>
> >     Sub Main()
>
> >         a = b = c = d = 0
> >         Console.WriteLine("{0} {1} {2} {3}", a, b, c, d)
>
> >     End Sub
>
> > End Module
>
> > I expected 0 0 0 0 to be printed to the console.
> > However, I got 255 0 0 0. Why is that?
> > I have tested the example with Mono VB compiler and
> >  Microsoft VB 2008 Express Edition.
>
> > Jan Bodnar

Reply via email to