0

Can someone explain what this line of code is actually doing.

txtModule.Visible = (cboModule.SelectedIndex = 0)

I am hoping it will set the visible property of txtModule to the boolean result of cboModule having a SelectedIndex of 0, but is this the case, and if so why.

I tried searching for an answer to this, but I do not know what this kind of assignment is called, so if you know that, it would also be helpful.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Graham
  • 318
  • 1
  • 16
  • You are simply assigning the result of an expression. `(cboModule.SelectedIndex = 0)` is a boolean expression that will return true or false depending on whether or not the index is equal to 0, the resulting boolean is then assigned to the `Visible` property. – Alex K. Nov 18 '14 at 14:09
  • Thanks, I was concerned that it would not always work as I expected, for example setting the value of cboModule.SelectedIndex to 0. – Graham Nov 18 '14 at 14:33
  • Its perfectly fine although as pointed out below its less clear than say the C# equivalent `txtModule.Visible = (cboModule.SelectedIndex == 0)` where the assignment and equality are different operators. – Alex K. Nov 18 '14 at 14:47

4 Answers4

2

It's the same as

If cboModule.SelectedIndex = 0 Then
    txtModule.Visible = True
Else
    txtModule.Visible = False
End If

Since comparing cboModule.SelectedIndex with 0 returns a Boolean, you could also do.

Dim b As Boolean
b = (cboModule.SelectedIndex = 0)
txtModule.Visible = b

In VB it can be a little confusing since both setting a variable and comparing it uses the same operator.

the_lotus
  • 12,668
  • 3
  • 36
  • 53
0

Alex K's comment is correct. You could more verbosely write that statement as

txtModule.Visible = If(cboModule.SelectedIndex = 0, True, False)

which would do exactly the same thing.

LeeG
  • 708
  • 5
  • 14
0

The trick here is that the = operator has two different meanings in VB.Net; it can be used both for equality comparisons, and for assignment. Furthermore, there are a number of situations where assignments can be made as part of larger expressions, such that an assignment doesn't require that the variable being assigned to is all the way on the left of the line of code.

In this case, we have two = expressions. It's clear the first expression (the = on the left) is an assignment to the boolean txtModule.Visible variable, but the cboModule.SelectedIndex = 0 expression is less clear. In different contexts, this could be both assignment or an equality test. The purpose of the parentheses, then is to try to clarify that the expression is a comparison, likely written by someone who is used to C-style languages, where parentheses are commonly used to signify conditional expressions. The result of the comparison (a Boolean) is then assigned to the Boolean txtModule.Visible variable.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
0

The real answer:

    txtModule.Visible = (cboModule.SelectedIndex = 0)

is actually

    Let txtModule.Visible = (cboModule.SelectedIndex = 0)

where the equals sign plays two roles.

In the first role, it is part of the syntax of Let, as in Let variable = expression. The Let is rarely used, because as BASIC evolved, it became optional (see http://msdn.microsoft.com/en-us/library/aa243390(v=vs.60).aspx). This is just how BASIC (and VB6 and VBA) does its assignment statement (see http://en.wikipedia.org/wiki/Assignment_(computer_science) ).

Actually, VB.NET doesn't use Let, but the usage of equals sign in this first role continues on.

In the second role, it is the equality operator, generally equivalent to == of C# (but not exactly, see Object equality behaves different in .NET )

In some languages, assignment is a statement (such as VB.Net), and in others it's an operator (such as C#). Per the Wiki page cited above: "The use of the equals sign as an assignment operator has been frequently criticized, due to the conflict with equals as comparison for equality. This results both in confusion by novices in writing code, and confusion even by experienced programmers in reading code." Apparently, Fortran is to blame.

Community
  • 1
  • 1
rskar
  • 4,607
  • 25
  • 21