Array of Booleans does not set Boolean variables?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Dim bNcd, bNcm, bNqa, bNcur, bN0, bN1, bN2, bN3, bN4 As Boolean
Dim arrBool As Boolean() = {bNcd, bNcm, bNqa, bNcur, bN0, bN1, bN2, bN3, bN4}
Dim i As Integer

bNcd = True
bNcm = True
bNqa = True
bNcur = True
bN0 = True
bN1 = True
bN2 = True
bN3 = True
bN4 = True

For i = 0 To arrBool.Length - 1
Console.WriteLine(arrBool(i)) '--these are all False instead of True
Next

For i = 0 To arrBool.Length - 1 : arrBool(i) = False : Next

'--these are still True
Console.WriteLine("bNcd is " & bNcd)
Console.WriteLine("bNcm is " & bNcm)
Console.WriteLine("bNqa is " & bNqa)
Console.WriteLine("bNcur is " & bNcur)
Console.WriteLine("bN0 is " & bN0)
Console.WriteLine("bN1 is " & bN1)
Console.WriteLine("bN2 is " & bN2)
Console.WriteLine("bN3 is " & bN3)
Console.WriteLine("bN4 is " & bN4)

Is there a way to set boolean vars from an array?

Thanks,
Rich
 
Rich said:
Dim bNcd, bNcm, bNqa, bNcur, bN0, bN1, bN2, bN3, bN4 As Boolean
Dim arrBool As Boolean() = {bNcd, bNcm, bNqa, bNcur, bN0, bN1, bN2, bN3,
bN4}
Dim i As Integer

bNcd = True
bNcm = True
bNqa = True
bNcur = True
bN0 = True
bN1 = True
bN2 = True
bN3 = True
bN4 = True

For i = 0 To arrBool.Length - 1
Console.WriteLine(arrBool(i)) '--these are all False instead of True
Next

'Boolean' is a value type, and thus 'arrBool' doesn't store references to
the boolean variables. Instead, a copies of their values are stored in the
array.
Is there a way to set boolean vars from an array?

You could use an array variable instead of individual variables.
 
Rich said:
Dim bNcd, bNcm, bNqa, bNcur, bN0, bN1, bN2, bN3, bN4 As Boolean
Dim arrBool As Boolean() = {bNcd, bNcm, bNqa, bNcur, bN0, bN1, bN2, bN3,
bN4}
Dim i As Integer

bNcd = True
bNcm = True
bNqa = True
bNcur = True
bN0 = True
bN1 = True
bN2 = True
bN3 = True
bN4 = True

For i = 0 To arrBool.Length - 1
Console.WriteLine(arrBool(i)) '--these are all False instead of True
Next

For i = 0 To arrBool.Length - 1 : arrBool(i) = False : Next

'--these are still True
Console.WriteLine("bNcd is " & bNcd)
Console.WriteLine("bNcm is " & bNcm)
Console.WriteLine("bNqa is " & bNqa)
Console.WriteLine("bNcur is " & bNcur)
Console.WriteLine("bN0 is " & bN0)
Console.WriteLine("bN1 is " & bN1)
Console.WriteLine("bN2 is " & bN2)
Console.WriteLine("bN3 is " & bN3)
Console.WriteLine("bN4 is " & bN4)

Is there a way to set boolean vars from an array?

Thanks,
Rich

This is because, boolean values are value-types, not reference types. When
you change the value of a value-type, only the value you changed is updated.

Dim a As Boolean = True
Dim b As Boolean = a

a = False

You set the "value" of a to True.
You then set the "value" of b to the "value" of a, which is True.
You set the "value" of a to False.

The "value" of b is still True.

Dim a As ArrayList = New ArrayList()
a.Add(True)
Dim b As ArrayList = a

a.Clear()
a.Add(False)

You set the "reference" of a to a new array list and add the value True.
You set the "reference" of b to the "reference" of b.
You clear the "reference" values.
You add the value False to the "reference" a.

The "reference" of b still points to the same ArrayList instance of a.
Therefore the ArrayList of b is the same as a and contains the "value" of
False.

Understand?

HTH,
Mythran

(Did not use technical terms as this may prevent understanding the basics of
reference types vs value types...)
 
Rich said:
Dim bNcd, bNcm, bNqa, bNcur, bN0, bN1, bN2, bN3, bN4 As Boolean
Dim arrBool As Boolean() = {bNcd, bNcm, bNqa, bNcur, bN0, bN1, bN2, bN3, bN4}
Dim i As Integer

bNcd = True
bNcm = True
bNqa = True
bNcur = True
bN0 = True
bN1 = True
bN2 = True
bN3 = True
bN4 = True

For i = 0 To arrBool.Length - 1
Console.WriteLine(arrBool(i)) '--these are all False instead of True
Next

You did not change the array content. You changed the single variables.

For i = 0 To arrBool.Length - 1 : arrBool(i) = False : Next

'--these are still True
Console.WriteLine("bNcd is " & bNcd)
Console.WriteLine("bNcm is " & bNcm)
Console.WriteLine("bNqa is " & bNqa)
Console.WriteLine("bNcur is " & bNcur)
Console.WriteLine("bN0 is " & bN0)
Console.WriteLine("bN1 is " & bN1)
Console.WriteLine("bN2 is " & bN2)
Console.WriteLine("bN3 is " & bN3)
Console.WriteLine("bN4 is " & bN4)

Is there a way to set boolean vars from an array?

You would have to copy it back to the array.

Armin
 
Thank you all for your replies and explanations. Yes, it makes sense.
Fundamental stuff about references and Value. Need to brush up. I was just
checking if there were a more efficient way to do what I want instead of

arrBool(0) = False
arrBool(1) = False
....

I guess I could go the way of the ArrayList. Might try that.

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top