locked
"An unhandled exception of type 'System.IndexOutOfRangeException" RRS feed

  • Question

  • <span style="font-family:Verdana,Arial,Helvetica,sans-serif; white-space:normal"><pre lang="x-c#" style="font-size:11px; border:1px solid #d0d0d0">i am beginner in .net,following is a code of my console application.here i am trying to create objects of class "my account" that will create multiple objects. but when i run my project i get an error "An unhandled exception of type 'System.IndexOutOfRangeException". 
    namespace Bank Application
    
    
    {
      class Program
      {
        static void Main(string[] args)
        {
        int i = 0;
          Console.WriteLine("...:::::Select your choice:::::....\n\n1.Create an Account\n2.With Draw\n3.Deposit\n4.Check Balance");
          int choice = int.Parse(Console.ReadLine());
          switch (choice)
          {
            case 1:
              Console.WriteLine("Welcome to Create an Account");
              account(i);
              break;
            default:
              Console.WriteLine("Please Select Valid Option");
              break;
          }
          Console.ReadLine();
    
        }
    
        public static void account(int i)
        {
          Account.MyAccount[] acc1 = new Account.MyAccount[i];
          Console.WriteLine("\nName : "); acc1[i].name = Console.ReadLine();
          Console.WriteLine("\nFather Name: "); acc1[i].fname = Console.ReadLine();
          Console.WriteLine("\nNIC :");acc1[i].nic = Console.ReadLine();
          Random rand = new Random();
          acc1[i].acc_no = rand.Next(1000);
          Console.WriteLine("\nPress any key to see your Account Information : ");
          Console.WriteLine("\nAccount # : {0}\nName : {1}\nFather Name : {2}\nN.I.C : {3}",acc1[i].acc_no,acc1[i].name,acc1[i].fname,acc1[i].nic);
          i++;
          Console.Read();
          
        }
    
    
      }
      }
    namespace Account
    {
       public class MyAccount
      {
        public string name;public string fname;public string nic;public int acc_no;
      }
    }
    

    Friday, February 11, 2011 7:27 PM

Answers

  • I did some modifications in your code. It will help you in learning of C# even depper. 

    Take a look and let me know what do you thing. 

    ps: code allows you to do continous jobs, until you want to. Take a look:

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Feb11BankApplication
    {
      class Program
      {
        static void Main(string[] args)
        {     
          List<Account.MyAccount> list = new List<Account.MyAccount>();
    
          while (true)
          {
            Console.WriteLine("...:::::Select your choice:::::....\n\n1.Create an Account\n2.With Draw\n3.Deposit\n4.Check Balance");
            Console.WriteLine("(If you wish to quit, write exit)");
    
            string _choice = Console.ReadLine();
            if (_choice == "exit")
              break;
            else
            {
              int intChoice = 0;
              bool bChecking = int.TryParse(_choice, out intChoice);
              if (bChecking)
              {
                switch (intChoice)
                {
                  case 1:
                    {
                      Console.WriteLine("Welcome to Create an Account");
                      list = account(list);
                      break;
                    }
                  default:
                    {
                      Console.WriteLine("Please Select Valid Option");
                      break;
                    }
                }
                ShowDetails(list);
              }
              else
                Console.WriteLine("Wrong choice. Please do a new selection...");
            }
          }
        }
    
        private static void ShowDetails(List<Account.MyAccount> list)
        {      
          //int i = 0;
          foreach (Account.MyAccount account in list)
          {
            Console.WriteLine("Account No.{0}", list.FindIndex(a => a.name == account.name));
            Console.WriteLine("Account # : {3}\nName : {0}\nFather Name : {1}\nN.I.C : {3}", account.name, account.fname, account.nic, account.acc_no);
          }
          Console.WriteLine("------------------------------");
        }
    
        public static List<Account.MyAccount> account(List<Account.MyAccount> list)
        {
          Account.MyAccount newAccount = new Account.MyAccount();
    
          Console.WriteLine("\nName : ");
          newAccount.name = Console.ReadLine();
    
          Console.WriteLine("\nFather Name: ");
          newAccount.fname = Console.ReadLine();
    
          Console.WriteLine("\nNIC :");
          newAccount.nic = Console.ReadLine();
    
          Random rand = new Random();
          newAccount.acc_no = rand.Next(1000);
    
          list.Add(newAccount);
          return list;
        }
      }
    }
    namespace Account
    {
      public class MyAccount
      {
        public string name { get; set; }
        public string fname; 
        public string nic;
        public int acc_no;
      }
    }
    
    

    Hope it helps,

    Mitja

    • Proposed as answer by Paul Zhou Tuesday, February 15, 2011 6:18 AM
    • Marked as answer by Paul Zhou Monday, February 21, 2011 2:23 AM
    Friday, February 11, 2011 8:13 PM

All replies

  • Why dont you have all classes in the same namespace (in one, not in two)?

     

    This should doint: 

        public static void account(int i)
        {
          Account.MyAccount[] acc1 = new Account.MyAccount[i + 1];
           //the rest of the code
        }
    you have to rise the array by one, since now is 0 - which cannot be used (it has to be at least 1).

    • Edited by Mitja Bonca Friday, February 11, 2011 8:18 PM
    Friday, February 11, 2011 7:39 PM
  • You're using this like this:

    // Create an array of "i" elements, ie: 5 elements
    Account.MyAccount[] acc1 = new Account.MyAccount[i];
    
    ... 
    
    // Here you're trying to access the 6th element!!! This will fail
    acc1[i].name = ...
    

    You need to allocate i+1, since array access is 0 based.  ([0] is the first element, [5] is the 6th, etc).

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, February 11, 2011 7:55 PM
    Moderator
  • I did some modifications in your code. It will help you in learning of C# even depper. 

    Take a look and let me know what do you thing. 

    ps: code allows you to do continous jobs, until you want to. Take a look:

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Feb11BankApplication
    {
      class Program
      {
        static void Main(string[] args)
        {     
          List<Account.MyAccount> list = new List<Account.MyAccount>();
    
          while (true)
          {
            Console.WriteLine("...:::::Select your choice:::::....\n\n1.Create an Account\n2.With Draw\n3.Deposit\n4.Check Balance");
            Console.WriteLine("(If you wish to quit, write exit)");
    
            string _choice = Console.ReadLine();
            if (_choice == "exit")
              break;
            else
            {
              int intChoice = 0;
              bool bChecking = int.TryParse(_choice, out intChoice);
              if (bChecking)
              {
                switch (intChoice)
                {
                  case 1:
                    {
                      Console.WriteLine("Welcome to Create an Account");
                      list = account(list);
                      break;
                    }
                  default:
                    {
                      Console.WriteLine("Please Select Valid Option");
                      break;
                    }
                }
                ShowDetails(list);
              }
              else
                Console.WriteLine("Wrong choice. Please do a new selection...");
            }
          }
        }
    
        private static void ShowDetails(List<Account.MyAccount> list)
        {      
          //int i = 0;
          foreach (Account.MyAccount account in list)
          {
            Console.WriteLine("Account No.{0}", list.FindIndex(a => a.name == account.name));
            Console.WriteLine("Account # : {3}\nName : {0}\nFather Name : {1}\nN.I.C : {3}", account.name, account.fname, account.nic, account.acc_no);
          }
          Console.WriteLine("------------------------------");
        }
    
        public static List<Account.MyAccount> account(List<Account.MyAccount> list)
        {
          Account.MyAccount newAccount = new Account.MyAccount();
    
          Console.WriteLine("\nName : ");
          newAccount.name = Console.ReadLine();
    
          Console.WriteLine("\nFather Name: ");
          newAccount.fname = Console.ReadLine();
    
          Console.WriteLine("\nNIC :");
          newAccount.nic = Console.ReadLine();
    
          Random rand = new Random();
          newAccount.acc_no = rand.Next(1000);
    
          list.Add(newAccount);
          return list;
        }
      }
    }
    namespace Account
    {
      public class MyAccount
      {
        public string name { get; set; }
        public string fname; 
        public string nic;
        public int acc_no;
      }
    }
    
    

    Hope it helps,

    Mitja

    • Proposed as answer by Paul Zhou Tuesday, February 15, 2011 6:18 AM
    • Marked as answer by Paul Zhou Monday, February 21, 2011 2:23 AM
    Friday, February 11, 2011 8:13 PM