473,382 Members | 1,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

index was outside the bounds of array

3
Hi! I want to ask if anyone can help me with my problem. I'm trying to write a program for an address book in C# using arrays.I'm still in the beginning and already have a problem.
This is the code i've written so far :

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Hope
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             string str1, str2, str3, str4, str5;
  13.  
  14.             Console.WriteLine("Enter Family name");
  15.             string s1 = Console.ReadLine();
  16.  
  17.             Console.WriteLine("Enter first name");
  18.             string s2 = Console.ReadLine();
  19.  
  20.             Console.WriteLine("Enter road number");
  21.             string s3 = Console.ReadLine();
  22.  
  23.             Console.WriteLine("Enter road name");
  24.             string s4 = Console.ReadLine();
  25.  
  26.             Console.WriteLine("Enter town name");
  27.             string s5 = Console.ReadLine();
  28.  
  29.             string[,] addressBook = { { s1 }, { s2 }, { s3 }, { s4 }, { s5 } };
  30.  
  31.             for (int i = 0; i < addressBook.Length / 2; i++)
  32.             {
  33.                 str1 = addressBook[i, 0];
  34.                 str2 = addressBook[i, 1];
  35.                 str3 = addressBook[i, 2];
  36.                 str4 = addressBook[i, 3];
  37.                 str5 = addressBook[i, 4];
  38.  
  39.                 Console.WriteLine(" {0}, {1}, {2}, {3}, {4} ", str1, str2, str3, str4, str5);
  40.                 Console.ReadLine();
  41.             }
  42.             Console.WriteLine();
  43.  
  44.         }
  45.     }
  46. }
In the for loop when the program goes to this line
Expand|Select|Wrap|Line Numbers
  1.                 str2 = addressBook[i, 1];
it breaks and shows me this message "Index was outside the bounds of the array."

I can't understand why it's happening and can't figure out a way to fix it.

I'll be happy for any help you can give me .
P.S. I'm a novice with C#.
Nov 30 '11 #1
5 3526
adriancs
122 100+
well, "string[,]" is double array.
single array, "string[]" is good enough.
try this:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             string str1, str2, str3, str4, str5;
  13.  
  14.             Console.Write("Enter Family name: ");
  15.             string s1 = Console.ReadLine();
  16.  
  17.             Console.Write("Enter first name: ");
  18.             string s2 = Console.ReadLine();
  19.  
  20.             Console.Write("Enter road number: ");
  21.             string s3 = Console.ReadLine();
  22.  
  23.             Console.Write("Enter road name: ");
  24.             string s4 = Console.ReadLine();
  25.  
  26.             Console.Write("Enter town name: ");
  27.             string s5 = Console.ReadLine();
  28.  
  29.             string[] addressBook = { s1, s2, s3, s4, s5 };
  30.  
  31.             for (int i = 0; i < addressBook.Length; i++)
  32.             {
  33.                 str1 = addressBook[0];
  34.                 str2 = addressBook[1];
  35.                 str3 = addressBook[2];
  36.                 str4 = addressBook[3];
  37.                 str5 = addressBook[4];
  38.  
  39.                 Console.WriteLine("\nResult: {0}, {1}, {2}, {3}, {4} ", str1, str2, str3, str4, str5);
  40.                 Console.ReadLine();
  41.             }
  42.             Console.WriteLine();
  43.         }
  44.     }
  45. }
if you wish to store more than 1 record dynamically, you can use "List<>".

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             List<string[]> lst = new List<string[]>();
  13.  
  14.             bool ExitProgram = false;
  15.  
  16.             while (!ExitProgram)
  17.             {
  18.                 Console.Clear();
  19.                 Console.WriteLine("Total records: " + lst.Count);
  20.                 Console.WriteLine("\nAdd new record\n");
  21.                 string str1, str2, str3, str4, str5;
  22.  
  23.                 Console.Write("Enter Family name: ");
  24.                 string s1 = Console.ReadLine();
  25.  
  26.                 Console.Write("Enter first name: ");
  27.                 string s2 = Console.ReadLine();
  28.  
  29.                 Console.Write("Enter road number: ");
  30.                 string s3 = Console.ReadLine();
  31.  
  32.                 Console.Write("Enter road name: ");
  33.                 string s4 = Console.ReadLine();
  34.  
  35.                 Console.Write("Enter town name: ");
  36.                 string s5 = Console.ReadLine();
  37.  
  38.                 string[] addressBook = { s1, s2, s3, s4, s5 };
  39.  
  40.                 lst.Add(addressBook);
  41.  
  42.                 Console.WriteLine();
  43.  
  44.                 for (int i = 0; i < lst.Count; i++)
  45.                 {
  46.                     str1 = addressBook[0];
  47.                     str2 = addressBook[1];
  48.                     str3 = addressBook[2];
  49.                     str4 = addressBook[3];
  50.                     str5 = addressBook[4];
  51.  
  52.                     Console.WriteLine("Record " + (i + 1) + ": {0}, {1}, {2}, {3}, {4} ", str1, str2, str3, str4, str5);
  53.                 }
  54.                 Console.Write("\nPress any key to continue, or [Q] to exit: ");
  55.  
  56.                 string nextTask = Console.ReadLine().ToUpper();
  57.  
  58.                 if (nextTask == "Q")
  59.                     ExitProgram = true;
  60.             }
  61.         }
  62.     }
  63. }
Dec 1 '11 #2
qjmigo
3
Thanks adriancs! This is really helpful but can you tell me why str2 = addressBook[i, 1]; is out of the bounds if the array? I can't figure that out and I want to understand it so I won't make the same mistake in the future.
Dec 3 '11 #3
adriancs
122 100+
Hi, qjmigo,

lets say, we create 5 strings:

Expand|Select|Wrap|Line Numbers
  1. string a1, b1, c1, d1, e1;
because this >> "[,]" is only 2 dimensional array, we can understand it with a 2-D graph.
for example, columns and rows.

so, if we initialize the string array with this:

Expand|Select|Wrap|Line Numbers
  1. string[,] addressBook = { { a1 }, { b1 }, { c1 }, { d1 }, { e1 } };
will create 5 rows with 1 column. Lets asume Y = row, and X = column.

Expand|Select|Wrap|Line Numbers
  1.     X1
  2. Y1    a1
  3. Y2    b1
  4. Y3    c1
  5. Y4    d1
  6. Y5    e1
as you can see, addressBook only contains 5 strings.
results:
Expand|Select|Wrap|Line Numbers
  1. addressBook.Length = 5;
  2. addressBook[0,0] = a1;
  3. addressBook[3,0] = d1;
lets try another example. We initialize the string array with this:
Expand|Select|Wrap|Line Numbers
  1. string a1, a2, a3, b1, b2, b3, c1, c2, c3, d1, d2, d3, e1, e2, e3;
  2. string[,] addressBook = { { a1, a2, a3 }, { b1, b2, b3 }, { c1, c2, c3 }, { d1, d2, d3 }, { e1, e2, e3 } };
this will create 5 rows and 3 columns,

Expand|Select|Wrap|Line Numbers
  1.     X1    X2    X3
  2. Y1    a1    a2    a3
  3. Y2    b1    b2    b3
  4. Y3    c1    c2    c3    
  5. Y4    d1    d2    d3
  6. Y5    e1    e2    e3
Results:

Expand|Select|Wrap|Line Numbers
  1. addressBook.Length = 15;
  2. addressBook[0,0] = a1;
  3. addressBook[2,1] = c2;
  4. addressBook[4,2] = e3;
  5. addressBook[5,0] = outside the bounds of array, there is no Row 6.
  6. addressBook[0,3] = outside the bounds of array, there is no Column 4.
now we come back to your codes, this block:
Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < addressBook.Length / 2; i++)
  2. {
  3.  
  4. }
in this case:
Expand|Select|Wrap|Line Numbers
  1. string[,] addressBook = { { s1 }, { s2 }, { s3 }, { s4 }, { s5 } };
  2. addressBook.Length = 5;
which means:
Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < (5 / 2); i++)
  2. {
  3.  
  4. }
then

Expand|Select|Wrap|Line Numbers
  1. addressBook[i, 0]; << exist
  2. addressBook[i, 1]; << there is no column 2
  3. addressBook[i, 2]; << there is no column 3
  4. addressBook[i, 3]; << there is no column 4
  5. addressBook[i, 4]; << there is no column 5
and because your loop condition is " i < (5 / 2) "
this "for loop" will have at least 3 loops.
first loop, i = 0
this loop is ok. the number zero refers to first row.
but start from 2nd loop, i = 1, refers to 2nd row.
but there is no 2nd row, thus, create another error >> outside the bounds of array.
3rd loop, i = 2, refers to 3rd row.
>> outside the bounds of array. There is no row 3.
Dec 5 '11 #4
qjmigo
3
adriancs that is a really rich answer! Now I understand where is the problem. Thanks a lot! :-)
Dec 10 '11 #5
adriancs
122 100+
Hi, you are welcome.

by the way, you may consider to mark "choose as best answer button" on the most appropriate post that has solved your problems. :-)
thanks
Dec 11 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Joe | last post by:
This is a weird problem. when doing trying to add a tabpage to a tabcontrol I get an error: Index was outside the bounds of the array. Now this only happens when I call a function from within...
3
by: ORC | last post by:
I have a large array where the first index must be removed, but how is the best way to do that when the application must be optimized to speed? Thanks Ole
2
by: B-Dog | last post by:
Any idea why when I try to fill my dataset using xContacts.Fill(xds.Contacts) I get the error: : Index was outside the bounds of the array. I've tried rebuilding my dataadapters and dataset...
1
by: Allen Maki | last post by:
Why can I get the index of the item of the array when I use string*, but can not get the index of the array when I use any other type (such as Int32)? This code will compile perfectly, but if I...
6
by: routeslip | last post by:
I'm refering to an entry in an array by it's string key, as in foo. Is there a way to get the numeric index of that array without iterating through the entire array? What I need to do is find...
3
by: writebrent | last post by:
I wrote a little interface for users to post data to a website. On their local machines, it produces CSV from an Excel spreadsheet, then posts it to the site. In some cases, the CSV will contain...
3
by: Erwin Moller | last post by:
Hi, Consider the following code: ------------------------- function giveArr(){ return array("one","two","three"); } echo giveArr();
6
by: sgottenyc | last post by:
Hello, If you could assist me with the following situation, I would be very grateful. I have a table of data retrieved from database displayed on screen. To each row of data, I have added...
1
foolios
by: foolios | last post by:
private void CreateMaze(int h, int w) { int r = h * w; Room rooms = new Room; Random rm = new Random(); int newrm = rm.Next(0, r); ...
7
by: bouy78 | last post by:
Quick Question Seems to be an easy task, but apparently I'm not very bright. I have an array of checkboxes, created at runtime and inserted in a panel. Upon creation, they're each attached to...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.