Click here to Skip to main content
15,890,946 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
1).
C#
for (int i = 0; i < IsoMessageReceived.Length; i++)
{
  IsoMessageForProcessing[i + 2] = IsoMessageReceived[i];
}

2).
C#
int Length = Convert.ToInt32(Input[0]) * 256 + Convert.ToInt32(Input[1]);

byte[] ResultBytes = new byte[Length];
string ResultOutput = string.Empty;

for (int i = 0; i < Length; i++)
{
  ResultBytes[i] = Input[i + 2];        //Input[i + 2]
}

The above 2 codes are throwing the error

This is the error : Index was outside the bounds of the array

What I have tried:

i'm debugging this code but throwing me error
Posted
Updated 3-Jan-20 5:33am
v2
Comments
F-ES Sitecore 9-Jul-18 8:59am    
You get this when the number in the brackets refers to an item outside the array size

If "x" is an array of string of length 3

x[0] = "a"
x[1] = "b"
x[2] = "c"

If you reference anything but x[0], x[1] or x[2] you'll get that error

string y = x[3]

So look at the length of the arrays you are accessing, look at the indexes you are using (the numbers in the brackets) and try and work out why you are accessing pas the index boundaries. We can't access your code or inputs so we can't do that for you.
Shahbaz435 10-Jul-18 1:45am    
what i do plz suggest something
johannesnestler 10-Jul-19 9:48am    
After reading the Solutions and seeing your struggle to understand the answers you get, I'd suggest: forget about this code for a while and go back to a C# tutorial and read 1 hour about arrays and indices. This would help you more than asking Questions about an Exception that tells you exactly what the problem is. Index was outside the Bounds of the Array... Don't run before you walk - and btw. learn to use your Debugger to see what your array-size really is. Useful link: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/
CPallini 12-Sep-19 5:56am    
You should post more detailed info about your arrays (e.g. Input). Use the debugger to inspect them and then post here the relevant info (for instance, the values of Input[0] and Input[1] and the actual length of the Input array).

Look at what you are doing While your loop guard is right for ResultBytes, there is nothing there that says how big the Input array is. If it isn't long enough to cope with the Length + 2, you will get that exception as you run off the end of the array.

I'#d suggest that you look at Input and where it comes from: since it's bytes, I'd guess you are using a SerialPort and haven't received the entire message yet...
 
Share this answer
 
Comments
Shahbaz435 10-Jul-18 1:45am    
input array is not satisfied the condition it throws me error plz help
OriginalGriff 10-Jul-18 2:01am    
That tells us nothing - stop trying to type as little as possible and give us actual information.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Shahbaz435 10-Jul-18 2:06am    
IsoMessageReceived = streamReader.ReadBytes(Convert.ToInt32((FirstLetter * 256) + NextChars));
IsoMessageForProcessing = new byte[IsoMessageReceived.Length + 2];

IsoMessageForProcessing[0] = Convert.ToByte(FirstLetter);
IsoMessageForProcessing[1] = Convert.ToByte(NextChars);

for (int i = 0; i < IsoMessageReceived.Length; i++) //[IsoMessageReceived.Length : 172]
{
IsoMessageForProcessing[i + 2] = IsoMessageReceived[i];
//[IsoMessageReceived :{byte[172]]
}
Temp = ConvertNFSByteArraytoFinIso8583(IsoMessageForProcessing); //IsoMessageForProcessing {byte[174]}
OriginalGriff 10-Jul-18 2:11am    
OK, so I have a lump of code.
Now, what does it do that you didn't expect or not do that you did?
How does that relate to the other code?

Remember, I can't run your code: I don't have whatever the stream is connected to!
Shahbaz435 10-Jul-18 2:13am    
this is my code im getting the value in firstletter and nextchars when the loop is started it is satified with condition adding 2 more the value in that
You are running on i, but using i + 2 too...
Let imagine that you have 5 members in this case you will have these indexes...
i     -> 0  1  2  3  4
i + 2 -> 2  3  4  5  6

5 and 6 are indexes that out of the bound!!!
 
Share this answer
 
Comments
Shahbaz435 10-Jul-18 1:43am    
so what should i do for that code
OriginalGriff 10-Jul-18 2:00am    
*cough* different array *cough*
Shahbaz435 10-Jul-18 2:06am    
IsoMessageReceived = streamReader.ReadBytes(Convert.ToInt32((FirstLetter * 256) + NextChars));
IsoMessageForProcessing = new byte[IsoMessageReceived.Length + 2];

IsoMessageForProcessing[0] = Convert.ToByte(FirstLetter);
IsoMessageForProcessing[1] = Convert.ToByte(NextChars);

for (int i = 0; i < IsoMessageReceived.Length; i++) //[IsoMessageReceived.Length : 172]
{
IsoMessageForProcessing[i + 2] = IsoMessageReceived[i];
//[IsoMessageReceived :{byte[172]]
}
Temp = ConvertNFSByteArraytoFinIso8583(IsoMessageForProcessing); //IsoMessageForProcessing {byte[174]}
Shahbaz435 10-Jul-18 2:10am    
my length is 172 but the loops is exiting on 174 how to come this plz help
For the first code block , the size of the IsoMessageForProcessing array must be at least IsoMessageReceived.Length + 2. You did not show the code where the array is defined / sized. But it is not large enough because you get an error.

Similar for the second case: The Input array must be at least of size Length + 2. Assuming it is a byte array buffer and because the length is calculated from the first two bytes, it should be of size 0x10002 to avoid any out of bound errors.
 
Share this answer
 
Comments
Shahbaz435 10-Jul-18 1:44am    
can u plz help me to resolve this error
Jochen Arndt 10-Jul-18 2:46am    
Sorry, I can't because you did not showed enough code like where and how the array sizes are set and what the code finally should do.

General rule:
Array items can be accessed with indexes from zero to size - 1. Any other index will throw an out of bound error.
Shahbaz435 10-Jul-18 2:49am    
int Length = Convert.ToInt32(Input[0]) * 256 + Convert.ToInt32(Input[1]); // the length is 12336 and input is 165
byte[] ResultBytes = new byte[Length];

string ResultOutput = string.Empty;

for (int i = 0; i < Length; i++)
{
ResultBytes[i] = Input[i + 2]; //Input[i + 2]
}
Jochen Arndt 10-Jul-18 3:03am    
When your input array has 165 bytes, you can access elements up to index 164. Indexes up to 12336 are obviously way too large.

But I can't help without knowing what input is and where the data come from.
Shahbaz435 10-Jul-18 3:09am    
public string ConvertNFSByteArraytoFinIso8583(byte[] Input)

{
int Length = Convert.ToInt32(Input[0]) * 256 + Convert.ToInt32(Input[1]);
byte[] ResultBytes = new byte[Length];

string ResultOutput = string.Empty;

for (int i = 0; i < Input.Length; i++) //(int i = 0; i < Length; i++)
{
ResultBytes[i] = Input[i]; //Input[i + 2]
}

Length = ResultBytes.Length;

and where should it getting the 12336 can u help me for this

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900