Starters: Here is the error that gets generated:
Destination array was not long enough. Check destIndex and length, and the array’s lower bounds.
Code: C#, ASP.NET
Env: VS 2005
What I’m doing is using a BitArray to flip bits based on a certain condition of a TreeView. Right now I’m traversing a TreeView and if the node is checked from a child, I will flip an index in the BitArray. I have multiple TreeViews that I am traversing so I have multiple BitArrays. Once I get all the BitArrays populated, I will convert the bits to an integer value (encoded in a sense) and place them in an int array. Then the ultimate goal is to push the integer values to a database but obviously I can’t get that far. Sample code provided below:
BitArray MTRTECH = new BitArray(8);
BitArray MTRRD = new BitArray(200);
BitArray REVSE = new BitArray(100);
BitArray ETXA = new BitArray(100);
int[] conversion = new int[11];
ParentChildCheck(MTRTECHTreeView, MTRTECH);
MTRTECH.CopyTo(conversion, 7);
ParentChildCheck(MTRRDRTreeView, MTRRD);
MTRRD.CopyTo(conversion, 8); <================ Throws Error Here
ParentChildCheck(REVSECTreeView, REVSE);
REVSE.CopyTo(conversion, 9);
ParentChildCheck(EXTRATreeView, ETXA);
ETXA.CopyTo(conversion, 10);
protected void ParentChildCheck(TreeView parent, BitArray Changes)
{
TreeNode temp = new TreeNode();
for (int index = 0; index < parent.Nodes.Count; index++)
{
temp = parent.Nodes[index];
for (int index2 = 0; index2 < temp.ChildNodes.Count; index2++)
{
ChildCheck(temp.ChildNodes[index2],Changes,index2);
}
}
}
protected void ChildCheck(TreeNode node, BitArray Selection, int value)
{
message2 += node.Text;
Selection.Set(value, true);
counter++;
for (int index = 0; index < node.ChildNodes.Count; index++)
{
value++;
ChildCheck(node.ChildNodes[index],Selection,value);
}
}
Your source
BitArrayMTRRD is internally stored as an array of 7Int32s, your destination arrayconversionis an array of 11Int32s. When you perform the copy you are specifying a index of 8, this index is an index into the destination array, so the copy will overrun because your desintation is not long enough to contain all 7Int32sstarting at index 8.