Keith

Keith

  • NA
  • 36
  • 0

Object Array + Dynamic Sizing

Dec 30 2007 12:36 PM

Something is wrong with my code, could anyone help, thanks in advance.

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Bus
    {
        int busNo;
        string startDepot, endDepot;
        float departTime;

        public void Update()
        {
            Console.Write("Entert Bus No.:");
            busNo = int.Parse(Console.ReadLine());
            Console.Write("Enter Start depot: ");
            startDepot = Console.ReadLine();
            Console.Write("Enter End depot: ");
            endDepot = Console.ReadLine();
            Console.Write("Enter depart time: ");
            departTime = float.Parse(Console.ReadLine());

        }

        public void Display()
        {
            Console.WriteLine("Bus No. is " + busNo);
            Console.WriteLine("Start depot is " + startDepot);
            Console.WriteLine("End depot is " + endDepot);
            Console.WriteLine("Depart time is " + departTime);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            char choice;
            do
            {
                int num=0;

                Console.WriteLine("a) Update   b) Display  e) Exit");
                Console.Write("Enter choice: ");
                choice = char.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 'a':
                        Console.WriteLine("Enter Number of Bus");
                        num = int.Parse(Console.ReadLine());
                        Bus[] group1 = new Bus[num]; //dynamic sizing

                        for (int row = 0; row < num; row++)
                        {
                            group1[row] = new Bus();
                            group1[row].Update();
                        }
                        break;

                    case 'b':
                        Console.WriteLine("Display all Bus(es)");
                        Bus[] group1 = new Bus[num]; //Something is wrong here !
                    for (int row = 0; row < num; row++)
                    {
                        //group1[row] = new Bus();
                        group1[row].Display(); //stuck
                    }
                    break;

                    default: Console.WriteLine("No such choice"); break;
                }

            } while (choice != 'e');

        }
    }
}


Answers (12)