473,471 Members | 2,138 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

index was outside the bounds of array

3 New Member
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 3528
adriancs
122 New Member
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 New Member
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 New Member
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 New Member
adriancs that is a really rich answer! Now I understand where is the problem. Thanks a lot! :-)
Dec 10 '11 #5
adriancs
122 New Member
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: 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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.