472,958 Members | 1,998 Online

# take first 10 elements in a char array....

hey, I have a method that takes a char array of 10.

I have a char array of 30. how do I make it send the first 10, then the
next 10, then the final 10 ?

I need help with my looping skills. thanks.

I have this, but I get the index was out of bounds....

char[] char1 =
{'1','2','3','4','5','6,'7','8','9','0','1','2','3 ','4','5','6','7','8','9','0','1','2','3','4', '5','6','7','8','9','0','1'};
char[] char2 = new char[10];
for (int i = 0; i < myChar.Length; i = i + 9)
{
for (int j = 0; j <= 8; j++)
{
char2.SetValue([char1 + char2], j);
method(char2);
}
}
Jan 9 '07 #1
4 6946

"roger_27" <ro*****@discussions.microsoft.comwrote in message
news:9B**********************************@microsof t.com...
hey, I have a method that takes a char array of 10.

I have a char array of 30. how do I make it send the first 10, then the
next 10, then the final 10 ?

I need help with my looping skills. thanks.

I have this, but I get the index was out of bounds....

char[] char1 =
{'1','2','3','4','5','6,'7','8','9','0','1','2','3 ','4','5','6','7','8','9','0','1','2','3','4',
'5','6','7','8','9','0','1'};
char[] char2 = new char[10];
for (int i = 0; i < myChar.Length; i = i + 9)
{
for (int j = 0; j <= 8; j++)
{
char2.SetValue([char1 + char2], j);
method(char2);
}
}
One way:

char[] char1 = ... yer array w/30 elements ..;
char[] first10 = new char[10];
char[] mid10 = new char[10];
char[] last10 = new char[10];

Array.Copy(char1, 0, first10, 0, 10);
Array.Copy(char1, 10, mid10, 0, 10);
Array.Copy(char1, 20, last10, 0, 10);
first10 now contains the first 10 elements of the char1 character array.
mid10 now contains the middle 10 elements of the char1 character array.
last10 now contains the last 10 elements of the char1 character array.

:) HTH,
Mythran
Jan 9 '07 #2

"roger_27" <ro*****@discussions.microsoft.comwrote in message
news:9B**********************************@microsof t.com...
hey, I have a method that takes a char array of 10.

I have a char array of 30. how do I make it send the first 10, then the
next 10, then the final 10 ?

I need help with my looping skills. thanks.

I have this, but I get the index was out of bounds....

char[] char1 =
{'1','2','3','4','5','6,'7','8','9','0','1','2','3 ','4','5','6','7','8','9','0','1','2','3','4',
'5','6','7','8','9','0','1'};
char[] char2 = new char[10];
for (int i = 0; i < myChar.Length; i = i + 9)
{
for (int j = 0; j <= 8; j++)
{
char2.SetValue([char1 + char2], j);
method(char2);
}
}
Second way:

char[] char1 =
{'1','2','3','4','5','6,'7','8','9','0','1','2','3 ','4','5','6','7','8','9','0','1','2','3','4',
'5','6','7','8','9','0','1'};

char[] char2 = new char[10];
for (int i = 0; i < char1.Length; i += 10)
{
Array.Copy(char1, i, char2, 0, Math.Min(10, char1.Length - i) ); // Min
function protects from exception when there is less than 10 elements to
copy.
method(char2);
Array.Clear(char2, 0, 10);
}

Note that your char1 aray has 31 elements , so last "pack" of 10 elements
will have only 1st element set.
Jan 9 '07 #3
The answer depends among other things what "method" is going to do with it.

In your example you've defined the 30 element array so make it two
dimensional and pass each set of 10 individually. Or you can pass along a
starting element to "method" so it knows which element to start with in the
30 element array. Or as somebody has responded you can break it up but why
break it up if you've created it to begin with?

If you're intent on doing it the way you've posted may I recommend that you
test the "looping" part without trying to reference the array? You just
need to see the element values you get and trying to incorporate the array
is causing the out of bounds error. Simply output the value so you can see
the numbers. You will see the number is out of range but you won't get an
error... adjust the routine and repeat.

"roger_27" <ro*****@discussions.microsoft.comwrote in message
news:9B**********************************@microsof t.com...
hey, I have a method that takes a char array of 10.

I have a char array of 30. how do I make it send the first 10, then the
next 10, then the final 10 ?

I need help with my looping skills. thanks.

I have this, but I get the index was out of bounds....

char[] char1 =
{'1','2','3','4','5','6,'7','8','9','0','1','2','3 ','4','5','6','7','8','9','0','1','2','3','4',
'5','6','7','8','9','0','1'};
char[] char2 = new char[10];
for (int i = 0; i < myChar.Length; i = i + 9)
{
for (int j = 0; j <= 8; j++)
{
char2.SetValue([char1 + char2], j);
method(char2);
}
}

Jan 9 '07 #4

"roger_27" <ro*****@discussions.microsoft.comwrote in message
news:9B**********************************@microsof t.com...
hey, I have a method that takes a char array of 10.

I have a char array of 30. how do I make it send the first 10, then the
next 10, then the final 10 ?

I need help with my looping skills. thanks.

I have this, but I get the index was out of bounds....

char[] char1 =
{'1','2','3','4','5','6,'7','8','9','0','1','2','3 ','4','5','6','7','8','9','0','1','2','3','4',
'5','6','7','8','9','0','1'};
char[] char2 = new char[10];
for (int i = 0; i < myChar.Length; i = i + 9)
{
for (int j = 0; j <= 8; j++)
{
char2.SetValue([char1 + char2], j);
method(char2);
}
}
And yet another way that's diff than my first way :) This is pretty easy
but probably not very elegant...note is uses the variables from my previous
post:

first10 = (new string(char1)).ToCharArray(0, 10);

and to make it easier to read:

private char[] GetCharsInArray(
Array arr,
int startIdx,
int length
)
{
// either this line
return (new string(arr)).ToCharArray(startIdx, length);

// or these lines, which are 'more correct' and probably
// process faster. They DO process faster on larger
// character arrays (I assume) as they do a direct copy
// rather than creating a new string and then convert
// that string to an array as above.
char[] buff = new char[length];
Array.Copy(arr, startIdx, buff, 0, length);
return buff;
}

first10 = GetCharsInArray(char1, 0, 10);
mid10 = GetCharsInArray(char1, 10, 10);
last10 = GetCharsInArray(char1, 20, 10);

HTH,
Mythran
Jan 9 '07 #5

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