473,324 Members | 2,246 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Array of Booleans does not set Boolean variables?

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

Nov 21 '05 #1
4 4504
"Rich" <Ri**@discussions.microsoft.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2

"Rich" <Ri**@discussions.microsoft.com> wrote in message
news:17**********************************@microsof t.com...
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...)
Nov 21 '05 #3
Rich schrieb:
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
Nov 21 '05 #4
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.

"Rich" wrote:
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


Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Gerrit Holl | last post by:
Hi, is it proper to compare booleans? It is possible, of course, because they're compatible with numbers, but booleans aren't truly numbers. I'm tempted to write: return cmp(self.extends,...
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
20
by: Crirus | last post by:
I have a 20x16 array of objects. I need a way to iterate through the array and only consider few rows and few columns. To be clearer, sometimes I need only first 2 rows and last 3 columns to be...
6
by: chech | last post by:
Possible to have array of booleans? Dim b1 As Boolean, b2 As Boolean, b3 As Boolean Dim obj() As Object = {b1, b2, b3} dim v As Object For Each v In obj v = True Next This does not work. ...
4
by: cbdeja | last post by:
I am using a "unsigned long long" as a label to identify various transactions that are taking place in my program. The label is reserved during the lifetime of the transaction and then released...
10
by: cbdeja | last post by:
I am using a "unsigned long long" as a label to identify various transactions that are taking place in my program. The label is reserved during the lifetime of the transaction and then released...
10
by: alanbe | last post by:
I am working on some scripts to help me automate the website creation process. I want to use a template for the whole website and call pages using something like ...
32
by: Simon L | last post by:
BOOL bMyarray; memset(bMyArray , 0xFF, sizeof(BOOL) * 1000); clean & quick until someone else's code found I was returning -1 to mean true Because my BOOL is 4 bytes long, TRUE in memory...
14
by: cmdolcet69 | last post by:
I have setup a boolean based on the following conditions Public STRData as string Public BooleanRedFlag as boolean = False If STRData = "" then _BooleanRedFlag = TRue end If What should...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.