Click here to Skip to main content
15,885,864 members

Assign objects of class to elements of array and a for loop to call elements of array

gatordh7 asked:

Open original thread
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();
Tags: C#

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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