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);
}
} 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
"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.
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);
}
}
"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;
}
and in your code:
first10 = GetCharsInArray(char1, 0, 10);
mid10 = GetCharsInArray(char1, 10, 10);
last10 = GetCharsInArray(char1, 20, 10);
HTH,
Mythran This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Pontus F |
last post by:
Hi I am learning C++ and I'm still trying to get a grip of pointers
and other C/C++ concepts. I would appreciate if somebody could explain
what's wrong with this code:
---begin code block---
...
|
by: Mark Miller |
last post by:
I have a char array and when I write it to a file using BinaryWriter the
position of the pointer is the size of the array + 1. For example: writing
char leaves the pointer at position 26 after...
|
by: Luiz Antonio Gomes Picanço |
last post by:
I have this structure:
struct some
{
int version;
char signature;
} some2
void main()
{
|
by: Amrit Kohli |
last post by:
Hello.
I have the following code, to do a simple operation by copying the elements
of a vector of strings into an array of char pointers. However, when I run
this code, the first element in the...
|
by: rob.kirkpatrick |
last post by:
Hello
I need to populate an array of char arrays at run-time. A very
simplifed version of the code is below.
char ** list should contain cnt char arrays. The values of char ** list
are set by...
|
by: thinktwice |
last post by:
char a = { 0 }
is it ok?
|
by: OziRus |
last post by:
Hi,
I've char* array that I defined like char *str. I want to read
from a file, that contains names in each row, and assign them to my str
char * array.
m is a char array, f is a file...
|
by: Angus |
last post by:
Hello
I want to write a function which returns a char array.
But if I write this:
char myfunction()
{
return "123";
}
|
by: Superman859 |
last post by:
Hello everyone. Heads up - c++ syntax is killing me. I do quite well
in creating a Java program with very few syntax errors, but I get them
all over the place in c++. The smallest little things...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |