Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Set Method

BitArray.Set Method

Sets the bit at a specific position in the BitArray to the specified value.

[Visual Basic]
Public Sub Set( _
   ByVal index As Integer, _
   ByVal value As Boolean _
)
[C#]
public void Set(
 int index,
 bool value
);
[C++]
public: void Set(
 int index,
 bool value
);
[JScript]
public function Set(
   index : int,
 value : Boolean
);

Parameters

index
The zero-based index of the bit to set.
value
The Boolean value to assign to the bit.

Exceptions

Exception Type Condition
ArgumentOutOfRangeException index is less than zero.

-or-

index is greater than or equal to the number of elements in the BitArray.

Example

[Visual Basic, C#, C++] The following example shows how to set and get specific elements in a BitArray.

[Visual Basic] 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesBitArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a BitArray.
        Dim myBA As New BitArray(5)
        
        ' Displays the properties and values of the BitArray.
        Console.WriteLine("myBA values:")
        PrintIndexAndValues(myBA)
        
        ' Sets all the elements to true.
        myBA.SetAll(True)
        
        ' Displays the properties and values of the BitArray.
        Console.WriteLine("After setting all elements to true,")
        PrintIndexAndValues(myBA)
        
        ' Sets the last index to false.
        myBA.Set(myBA.Count - 1, False)
        
        ' Displays the properties and values of the BitArray.
        Console.WriteLine("After setting the last element to false,")
        PrintIndexAndValues(myBA)
        
        ' Gets the value of the last two elements.
        Console.WriteLine("The last two elements are: ")
        Console.WriteLine(ControlChars.Tab + "at index {0} : {1}", _
           myBA.Count - 2, myBA.Get(myBA.Count - 2))
        Console.WriteLine(ControlChars.Tab + "at index {0} : {1}", _
           myBA.Count - 1, myBA.Get(myBA.Count - 1))
    End Sub 'Main
    
    
    
    Public Shared Sub PrintIndexAndValues(myList As IEnumerable)
        Dim i As Integer = 0
        Dim myEnumerator As System.Collections.IEnumerator = _
           myList.GetEnumerator()
        While myEnumerator.MoveNext()
            Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
               + "{1}", i, myEnumerator.Current)
            i += 1
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' myBA values:
'     [0]:    False
'     [1]:    False
'     [2]:    False
'     [3]:    False
'     [4]:    False
' 
' After setting all elements to true,
'     [0]:    True
'     [1]:    True
'     [2]:    True
'     [3]:    True
'     [4]:    True
' 
' After setting the last element to false,
'     [0]:    True
'     [1]:    True
'     [2]:    True
'     [3]:    True
'     [4]:    False
' 
' The last two elements are: 
'     at index 3 : True
'     at index 4 : False 

[C#] 
using System;
using System.Collections;
public class SamplesBitArray  {

   public static void Main()  {

      // Creates and initializes a BitArray.
      BitArray myBA = new BitArray( 5 );

      // Displays the properties and values of the BitArray.
      Console.WriteLine( "myBA values:" );
      PrintIndexAndValues( myBA );

      // Sets all the elements to true.
      myBA.SetAll( true );

      // Displays the properties and values of the BitArray.
      Console.WriteLine( "After setting all elements to true," );
      PrintIndexAndValues( myBA );

      // Sets the last index to false.
      myBA.Set( myBA.Count - 1, false );

      // Displays the properties and values of the BitArray.
      Console.WriteLine( "After setting the last element to false," );
      PrintIndexAndValues( myBA );

      // Gets the value of the last two elements.
      Console.WriteLine( "The last two elements are: " );
      Console.WriteLine( "\tat index {0} : {1}", myBA.Count - 2, myBA.Get( myBA.Count - 2 ) );
      Console.WriteLine( "\tat index {0} : {1}", myBA.Count - 1, myBA.Get( myBA.Count - 1 ) );
   }


   public static void PrintIndexAndValues( IEnumerable myList )  {
      int i = 0;
      System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

myBA values:
    [0]:    False
    [1]:    False
    [2]:    False
    [3]:    False
    [4]:    False

After setting all elements to true,
    [0]:    True
    [1]:    True
    [2]:    True
    [3]:    True
    [4]:    True

After setting the last element to false,
    [0]:    True
    [1]:    True
    [2]:    True
    [3]:    True
    [4]:    False

The last two elements are: 
    at index 3 : True
    at index 4 : False
*/ 

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;

void PrintIndexAndValues( IEnumerable* myList )  {
   int i = 0;
   System::Collections::IEnumerator* myEnumerator = myList->GetEnumerator();
   while ( myEnumerator->MoveNext() )
      Console::WriteLine( S"\t[{0}]:\t{1}", __box(i++), myEnumerator->Current );
   Console::WriteLine();
}

int main()  {
   // Creates and initializes a BitArray.
   BitArray* myBA = new BitArray( 5 );

   // Displays the properties and values of the BitArray.
   Console::WriteLine( S"myBA values:" );
   PrintIndexAndValues( myBA );

   // Sets all the elements to true.
   myBA->SetAll( true );

   // Displays the properties and values of the BitArray.
   Console::WriteLine( S"After setting all elements to true," );
   PrintIndexAndValues( myBA );

   // Sets the last index to false.
   myBA->Set( myBA->Count - 1, false );

   // Displays the properties and values of the BitArray.
   Console::WriteLine( S"After setting the last element to false," );
   PrintIndexAndValues( myBA );

   // Gets the value of the last two elements.
   Console::WriteLine( S"The last two elements are: " );
   Console::WriteLine( S"\tat index {0} : {1}", __box(myBA->Count - 2), __box(myBA->Get( myBA->Count - 2 )) );
   Console::WriteLine( S"\tat index {0} : {1}", __box(myBA->Count - 1), __box(myBA->Get( myBA->Count - 1 )) );
}
/*
This code produces the following output.

myBA values:
        [0]:    False
        [1]:    False
        [2]:    False
        [3]:    False
        [4]:    False

After setting all elements to true,
        [0]:    True
        [1]:    True
        [2]:    True
        [3]:    True
        [4]:    True

After setting the last element to false,
        [0]:    True
        [1]:    True
        [2]:    True
        [3]:    True
        [4]:    False

The last two elements are:
        at index 3 : True
        at index 4 : False
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

BitArray Class | BitArray Members | System.Collections Namespace

Show:
© 2015 Microsoft