Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi everyone,

I'm fairly new here. This is my second post. I'm just learning how to use c#, and I was wondering if someone here can help me with arrays.

- First, I want to declare and create an array of 5 elements. I created another class called Account, and I want the array to be of type Account.

- Second, I want to assign the existing objects of the class I made (Account) to the elements of the array

- Lastly, I'm trying to figure out how to use a for loop to call the method display from each element of the array.

I've literally tried everything, and I just cant seem to get this. I want my new code to get the same results as my old code, but using an array (assigning the existing objects to the arrays elements) and a for loop to call the display method from each of the arrays elements.

I know first I need to declare a constant for max size, so I started off writing this

public const int MAX_SIZE = 5;

Then, to have an array declared with elements of the Account class, I wrote this under Main

int[] AccountArray = new Account[5];


At this point I'm lost. I tried looking online and in my textbook, but I can't find any examples specific to what I'd like to accomplish. Here is my original code that I want to modify


C#
static void Main(string[] args)

     {


        //int acctNo;

        Account acct = new Account(100, 2.5); // account #1 with $100 and 2.5% annual rate


        int choice;
      // int accountNo;
       double bal, r, amt; // balance, annual rate, amount
       int y; // number of years
       double fut; // future value


       Console.WriteLine("\nPlease make your selection:");
       Console.WriteLine("   0 - create an account");
       Console.WriteLine("   1 - deposit money");
       Console.WriteLine("   2 - withdraw money");
       Console.WriteLine("   3 - add monthly interest");
       Console.WriteLine("   4 - calculate future value");
       Console.WriteLine(" or -1 to finish");

       choice = Convert.ToInt32(Console.ReadLine());


        while (choice != -1)
       {
           switch (choice)
           {
               case 0: // create account

                  // acct.AccountDisplay();

                   Console.WriteLine("Enter balance:");
                   bal = Convert.ToDouble(Console.ReadLine());
                   Console.WriteLine("Enter percent of annual rate:");
                   r = Convert.ToDouble(Console.ReadLine());
                   acct = new Account(bal, r);
                   acct.display();

                   break;
               case 1: // deposit
                   Console.WriteLine("Enter deposit amount:");
                   amt = Convert.ToDouble(Console.ReadLine());
                   acct.deposit(amt);
                   acct.display();
                   break;

               case 2: // withdraw
                   Console.WriteLine("Enter withdrawal amount:");
                   amt = Convert.ToDouble(Console.ReadLine());
                   acct.withdraw(amt);
                   acct.display();
                   break;

               case 3: // add monthly interest
                   acct.applyMonthlyInterest();
                   acct.display();
                   break;

               case 4: // calculate future value
                   Console.WriteLine("Enter number of years for future value calculation:");
                   y = Convert.ToInt32(Console.ReadLine());
                   fut = acct.futureBalance(y);
                   Console.WriteLine("In {0} years you will have {1:c}." , y, fut);

                   break;

           } // end switch

           // next choice

           Console.WriteLine("\nPlease make your selection:");
           Console.WriteLine("   0 - create an account");
           Console.WriteLine("   1 - deposit money");
           Console.WriteLine("   2 - withdraw money");
           Console.WriteLine("   3 - add monthly interest");
           Console.WriteLine("   4 - calculate future value");
           Console.WriteLine(" or -1 to finish");

           choice = Convert.ToInt32(Console.ReadLine());
       }
       Console.WriteLine("Press any key, and good bye...");
       Console.ReadKey();
Posted
Comments
Jibesh 13-Mar-13 19:13pm    
I am not going to provide whole code - but a start up

int[] AccountArray = new Account[5]; this is wrong, you are trying to assign a wrong type hence the compiler error.

you should use like this.. give a try and I guess you are done.
Account[] AccountArray = new Account[5];

Firstly, if you want to create an array to store Account objects in it, it has to be the Account type, not int (actually it could be also for example System.Object type and that's related to class inheritance).

To assign an object to any element of the previously created array, you have you specify the index of the array, where it should be stored, like this:
C#
Account[] accounts = new Account[MAX_SIZE];
Account tempAcc = new Account(); //just to represent any Account object you've created
accounts[3] = tempAcc;


To call a certain method you have to loop thru the elements in the array, you can do it in 2 ways:
C#
for(int i = 0; i < tempAcc.Length; i++)
{
accounts[i].YourDesiredMethod();
}


or

C#
foreach(Account acc in accounts)
{
acc.YourDesiredMethod();
}
 
Share this answer
 
v2
I am not going to write the code but will try to answer how to do it.

Quote:
- First, I want to declare and create an array of 5 elements. I created another class called Account, and I want the array to be of type Account.
C#
Account[] accountArray  = new Account[MAX_SIZE]; // this is how you define an array in C#


Quote:

- Second, I want to assign the existing objects of the class I made (Account) to the elements of the array
to access the array you can use the index operator of the array like below
C#
Account account = accountArray[0];
this will get the account object from the 0th index. keep in mind that array index starts from zero not one.

Quote:

- Lastly, I'm trying to figure out how to use a for loop to call the method display from each element of the array.

To Iterate the array either you can use foreach method or for loop.
foreach(Account account in accountArray)
{
 //use the account object to execute the member function eg
 account.deposit(amount);
}
or
for(int i=0;i<max_size;i++)>
{
 Account account = accountArray[i];
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900