Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
Dear Sir,
 
how to solve Index was outside the bounds of the array in c# language.
 
note this is my Example:
 

1.
string[] Address1= {"CTS IT PARK","2nd Cross", "#87 Tambaram to Velachery Main Road", "Chennai- 73"};
this Array 4 values stored.
 
2.
 string[] Address2={"TCS IT PARK","Bangalore"};
this Array 2 values stored.
 
now i am using for - loop
 
for (i = 0, j = 0; i < address1.Length - 1 || j < address2.Length - 1; i++, j++)
{
WriteLine(address1[i],address2[j]);//Error. What can i do here.
}
 
but error in my loop Index was outside the bounds of the array. in c#
 
Give me solution how to solve this any alternate way then give me solution
 
By mohan(kainspire)
Posted 23-Dec-11 18:25pm
Edited 23-Dec-11 18:35pm
v7
Comments
SAKryukov at 24-Dec-11 0:34am
   
Did you delete you question when I already submitted an answer. This one:
http://www.codeproject.com/Questions/305052/How-to-write-code-for-checking-2-condition-in-Fore
If you did that, it's too bad. I spend good amount of time to help you, have written a big function...
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

So, what's so wonderful? You just have two array with different lengths. You index range goes outside one of the arrays. OK, I feel my previous answer could help. You previous question was deleted, which is too bad (who did it?!!).
 
You code simply does not have much sense.
 
However… this task makes some sense if made generalized. First, let's make an array element a generic parameters, because the problem does not really depend on it. Let's make the number of arrays arbitrary, number of elements in each array arbitrary and abstract our the conditions. Also, let's make a number of conditions arbitrary.
 
That can make some sense, at least remotely; and of course the ad-hoc problem would not have much sense. What, with hard-coded array ranges, etc.? Come on…
 
This set of input data will generate more then one method checking up the condition, because — how to define what is the check? I'll show just one of such predicate method, the one which validates that all conditions for all array elements are held true:
 
static bool AllConditionsValidForAllArrayElements<T>(
                T[][] arrays,
                System.Func<int,
                T, bool>[] conditions)
{
    foreach (T[] array in arrays) {
        int index = 0;
        foreach(T element in array)
            foreach (System.Func<int, T, bool> condition in conditions) {
                if (!condition(index, element))
                    return false;
                ++index;
            } //loop in conditions
    } // loop in arrays
    return true;
} //AllConditionsValidForAllArrayElements
 
[EDIT] Using the same principle, one could build different predicates on this data set, such as "at least one array element satisfies at least one condition", "at least one array element satisfies all conditions", "all elements of at least one array satisfy at least on condition", "all elements of at least one array satisfy all conditions", "at least one condition is satisfied by all array elements", etc. The one who can list all of them is at least a good student. Smile | :)
 
Only one problem here: the simplest analysis shows that the nested cycles is better in performance than any thinkable ways of merging them in one cycle, so what? I've warned about the fantasy in my comment to the first version of this question…
 
—SA
  Permalink  
v2
Comments
Rajesh Puli at 24-Dec-11 0:40am
   
Good Answer have my +5
SAKryukov at 24-Dec-11 0:53am
   
Thank you, Rajesh,
--SA
Amir Mahfoozi at 24-Dec-11 1:13am
   
+5
SAKryukov at 24-Dec-11 1:18am
   
Thank you, Amir. I wonder if you voted before or after the last fixes, after [EDIT]? :-)
--SA
Amir Mahfoozi at 24-Dec-11 1:22am
   
It was for nice effort to describe the situation for him. It worth five even before the edited part ;) .
MaulikDusara at 24-Dec-11 1:22am
   
My +5 too
--MD
SAKryukov at 24-Dec-11 16:51pm
   
Thank you, Maulik,
--SA
Monjurul Habib at 24-Dec-11 2:42am
   
5!
SAKryukov at 24-Dec-11 16:52pm
   
Thank you, Monjurul.
--SA
thatraja at 24-Dec-11 2:48am
   
5!
SAKryukov at 24-Dec-11 16:52pm
   
Thank you, Raja.
--SA
Little Soldier at 3-Apr-14 4:47am
   
how to use it?
AllConditionsValidForAllArrayElements
Sergey Alexandrovich Kryukov at 3-Apr-14 9:48am
   
Compare the length, apply some logic...
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Here address1 array has more item then address2 so you have index out of range problem
 
try below code.
 
for (i = 0, j = 0; i < address1.Length - 1 || j < address2.Length - 1; i++, j++)
{
if(j < address2.length)
{
   WriteLine(address1[i],address2[j]);
}
else
{
 WriteLine(address1[i],address2[address2.length-1]);
// or what ever you want to display.
}
 
}
 
Please let me know is this helpful to you?
  Permalink  

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

  Print Answers RSS
0 Maciej Los 450
1 OriginalGriff 403
2 Sergey Alexandrovich Kryukov 239
3 CPallini 189
4 CHill60 180
0 OriginalGriff 6,092
1 Sergey Alexandrovich Kryukov 4,972
2 Maciej Los 3,269
3 Peter Leow 3,129
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 3 Apr 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100