index was outside the bounds of array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • qjmigo
    New Member
    • Nov 2011
    • 3

    #1

    index was outside the bounds of array

    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 :

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Hope
        {
            static void Main(string[] args)
            {
                string str1, str2, str3, str4, str5;
    
                Console.WriteLine("Enter Family name");
                string s1 = Console.ReadLine();
    
                Console.WriteLine("Enter first name");
                string s2 = Console.ReadLine();
    
                Console.WriteLine("Enter road number");
                string s3 = Console.ReadLine();
    
                Console.WriteLine("Enter road name");
                string s4 = Console.ReadLine();
    
                Console.WriteLine("Enter town name");
                string s5 = Console.ReadLine();
    
                string[,] addressBook = { { s1 }, { s2 }, { s3 }, { s4 }, { s5 } };
    
                for (int i = 0; i < addressBook.Length / 2; i++)
                {
                    str1 = addressBook[i, 0];
                    str2 = addressBook[i, 1];
                    str3 = addressBook[i, 2];
                    str4 = addressBook[i, 3];
                    str5 = addressBook[i, 4];
    
                    Console.WriteLine(" {0}, {1}, {2}, {3}, {4} ", str1, str2, str3, str4, str5);
                    Console.ReadLine();
                }
                Console.WriteLine();
    
            }
        }
    }
    In the for loop when the program goes to this line
    Code:
                    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#.
    Last edited by NeoPa; Dec 7 '11, 02:51 AM. Reason: Added mandatory [CODE] tags for you
  • adriancs
    New Member
    • Apr 2011
    • 122

    #2
    well, "string[,]" is double array.
    single array, "string[]" is good enough.
    try this:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string str1, str2, str3, str4, str5;
    
                Console.Write("Enter Family name: ");
                string s1 = Console.ReadLine();
    
                Console.Write("Enter first name: ");
                string s2 = Console.ReadLine();
    
                Console.Write("Enter road number: ");
                string s3 = Console.ReadLine();
    
                Console.Write("Enter road name: ");
                string s4 = Console.ReadLine();
    
                Console.Write("Enter town name: ");
                string s5 = Console.ReadLine();
    
                string[] addressBook = { s1, s2, s3, s4, s5 };
    
                for (int i = 0; i < addressBook.Length; i++)
                {
                    str1 = addressBook[0];
                    str2 = addressBook[1];
                    str3 = addressBook[2];
                    str4 = addressBook[3];
                    str5 = addressBook[4];
    
                    Console.WriteLine("\nResult: {0}, {1}, {2}, {3}, {4} ", str1, str2, str3, str4, str5);
                    Console.ReadLine();
                }
                Console.WriteLine();
            }
        }
    }
    if you wish to store more than 1 record dynamically, you can use "List<>".

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<string[]> lst = new List<string[]>();
    
                bool ExitProgram = false;
    
                while (!ExitProgram)
                {
                    Console.Clear();
                    Console.WriteLine("Total records: " + lst.Count);
                    Console.WriteLine("\nAdd new record\n");
                    string str1, str2, str3, str4, str5;
    
                    Console.Write("Enter Family name: ");
                    string s1 = Console.ReadLine();
    
                    Console.Write("Enter first name: ");
                    string s2 = Console.ReadLine();
    
                    Console.Write("Enter road number: ");
                    string s3 = Console.ReadLine();
    
                    Console.Write("Enter road name: ");
                    string s4 = Console.ReadLine();
    
                    Console.Write("Enter town name: ");
                    string s5 = Console.ReadLine();
    
                    string[] addressBook = { s1, s2, s3, s4, s5 };
    
                    lst.Add(addressBook);
    
                    Console.WriteLine();
    
                    for (int i = 0; i < lst.Count; i++)
                    {
                        str1 = addressBook[0];
                        str2 = addressBook[1];
                        str3 = addressBook[2];
                        str4 = addressBook[3];
                        str5 = addressBook[4];
    
                        Console.WriteLine("Record " + (i + 1) + ": {0}, {1}, {2}, {3}, {4} ", str1, str2, str3, str4, str5);
                    }
                    Console.Write("\nPress any key to continue, or [Q] to exit: ");
    
                    string nextTask = Console.ReadLine().ToUpper();
    
                    if (nextTask == "Q")
                        ExitProgram = true;
                }
            }
        }
    }

    Comment

    • qjmigo
      New Member
      • Nov 2011
      • 3

      #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.

      Comment

      • adriancs
        New Member
        • Apr 2011
        • 122

        #4
        Hi, qjmigo,

        lets say, we create 5 strings:

        Code:
        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:

        Code:
        string[,] addressBook = { { a1 }, { b1 }, { c1 }, { d1 }, { e1 } };
        will create 5 rows with 1 column. Lets asume Y = row, and X = column.

        Code:
        	X1
        Y1	a1
        Y2	b1
        Y3	c1
        Y4	d1
        Y5	e1
        as you can see, addressBook only contains 5 strings.
        results:
        Code:
        addressBook.Length = 5;
        addressBook[0,0] = a1;
        addressBook[3,0] = d1;
        lets try another example. We initialize the string array with this:
        Code:
        string a1, a2, a3, b1, b2, b3, c1, c2, c3, d1, d2, d3, e1, e2, e3;
        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,

        Code:
        	X1	X2	X3
        Y1	a1	a2	a3
        Y2	b1	b2	b3
        Y3	c1	c2	c3	
        Y4	d1	d2	d3
        Y5	e1	e2	e3
        Results:

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

        Code:
        addressBook[i, 0]; << exist
        addressBook[i, 1]; << there is no column 2
        addressBook[i, 2]; << there is no column 3
        addressBook[i, 3]; << there is no column 4
        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.

        Comment

        • qjmigo
          New Member
          • Nov 2011
          • 3

          #5
          adriancs that is a really rich answer! Now I understand where is the problem. Thanks a lot! :-)

          Comment

          • adriancs
            New Member
            • Apr 2011
            • 122

            #6
            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

            Comment

            Working...