4

Is it possible in VB.NET doing a = b = 5? (I know that = is a comparison operator too)

I mean do not result (if b = 2 by e.g.)

a = false
b = 2

HOW to do it, however, in situations like bellow?


The inconvenient caused this question in my code: some objects a, b, .. z are passed by ref in a method, if I don't initialize them compiler warns me that it shoudl be initialized(= Nothing by e.g.)

Dim a, b, c, d, z As GraphicsPath ' = Nothing is impossible, only each a part
DrawPaths(a, b, c, d, z)          ' DrawPaths sets a = new GraphicPath() etc. 
serhio
  • 28,010
  • 62
  • 221
  • 374
  • 1
    The compiler is a bit stupid in this case, because you wouldn't need to initialize the a, b, c, d and z variables; they already have a value of Nothing. – Meta-Knight Jan 08 '10 at 14:55
  • @Meta-Knight: Yes... apparently there is no option in VB.NET like **out** – serhio Jan 08 '10 at 15:16

3 Answers3

9

a = b = 5 means

if b = 5 then a = true else a = false

if you want to assign the value 5 to a and to b at the same time, you must add it on a separate line :

b = 5
a = b

you can also write them on the same line but using the vb.net line separator :

b = 5 : a = b
Ali Tarhini
  • 5,278
  • 6
  • 41
  • 66
6

Because = in VB/VB.NET is also a comparison operator, so in that context it returns a boolean.

Will Vousden
  • 32,488
  • 9
  • 84
  • 95
2

That's just the rules of the Basic language. Many languages uses different operators to distinguish between assingment and equality testing.

For example,

  • C/C++/C#/Java uses = and ==.
  • Pascal uses := and =.
  • Basic does not.
Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Dan Byström
  • 9,067
  • 5
  • 38
  • 68