Resetting boolean variables in array

n3tw0rk

Member
Joined
Feb 3, 2006
Messages
11
Programming Experience
Beginner
I have a boolean variable declared in 3 (3,3) arrays, and I'm looking for the easiest to set all 9 in each array (or 16 depending on how you look at it, but I'm only using 9) to False. So basically.. is there a simple way or shortcut to setting a whole array to 1 value?
Thanks
 
The Array.Clear method will zero every element of an array. In the case of Boolean values, zeroing has the effect of setting them to False, so that would work in your specific case, e.g.:
VB.NET:
Array.Clear(myArray, 0, myArray.Length)
Generally speaking you would nee to use a loop, or nested loops for multi-dimensional arrays, to set each element to the value you want, e.g.:
VB.NET:
        For i As Integer = 0 To a.GetUpperBound(0)
            For j As Integer = 0 To a.GetUpperBound(1)
                myArray(i, j) = True
            Next j
        Next i
 
Alright thanks, I thought of nested loops to set them to false, but I figured there was an even easier way, which you have shown.
By the way, what is the a.GetUpperBound you have in your code, not sure I understand it... This is what I had before I saw your post
VB.NET:
[SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] x = 1 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] 3
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] y = 1 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] 3
Array1(x, y) = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2]Array2(x, y) = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2]Array3(x, y) = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE]

I have another question.. If I want to set a large number of objects (with similar names like obj1_1, obj1_2), etc, where there is a number correlation.. is there a way to use variables in object names? (for example obj1_x)? so I can create loops to do it...
 
GetUpperBound gets the highest index in the specifed dimension of an array. If you create an array like this:
VB.NET:
Dim myArray(12, 6, 8, 1) As Object
Then GetUpperBound would return the following values:

GetUpperBound(0) = 12
GetUpperBound(1) = 6
GetUpperBound(2) = 8
GetUpperBound(3) = 1

As for your second question, the only way to do it using variable names would be to use reflection. It is not really intended to be used like that though. It is more complex and inefficient than the situation calls for. You should just create an array with all the objects as elements and use a loop to iterate over them. The alternative would be to use a keyed collection, like a Hashtable or a SortedList, which would allow you to specify items by name. An array is more efficient though, so only use the collection if you specifically need to use names.
 
Yeah, but in my situation (just practicing and following examples), I used (3,3) instead of (2,2) for 1-3 because it was to my benefit for me editing the code.

And while I'm posting this, I could use some more help. I'm following one of the tutorials you posted, and I'm doing the tic-tac-toe example. But I'm having trouble coding a tie\cats in the game, which isn't included in the tutorial. Any help on that if you know what I'm talking about?
 
I'm afraid I don't know what you mean. I haven't read those tutorials in any detail myself. As for arrays and indexing, if you have other numbers that start at 1 then you would normally do something like this:
VB.NET:
For i As Integer =  oneBasedStartIndex To oneBasedEndIndex
    'access myArray(i - 1) here.
That way you still use the array elements from index zero but the elements correspond to some other entity that is 1-based.
 
yes, i agree with Jm totally, you should not create a 4 by 4 array if you really want a 3 by 3, it is messy coding and wastful, it's like buying 4 cars where only 3 are being driven around.
 
n3tw0rk said:
I don't really understand the code he posted...
As JB said, I was trying to show how you can access a zero-based array using one-based numbers. Here's a more concrete example:
VB.NET:
        Dim myArray As String() = {"string 1", "string 2", "string 3"}

        For i As Integer = 1 To 3
            MessageBox.Show(myArray(i - 1))
        Next i
This allows you to use one-based counting if you want (1, 2, 3) but does not leave the first lement unused, which is not really a good idea. It is unnessary, inefficient and could lead to errors if you ever use the Length property and forget to account for the fact that you are leaving the first element unused. Basically, it's best to just come to terms with the fact that's how things work, start thinking that way and abandon one-based counting altogether.

As an aside, I can tell you where this convention came from. It stems from the fact that these indices were originally a memory offset. When you are dealing with an array of bytes, the array itself will have a specific location in memory. The memory location of the first byte is offset by zero bytes from that location, the second is offset by one byte and so on. It may help you to think of array and collection indices as an offset from the beginning, because that's exactly what they mean. Then again it may not help. Either way, it's something you'll need to get used to.
 
Back
Top