C#++函数不起作用。 Int stay = 1

时间:2015-10-16 06:58:05

标签: c#

为什么我一直得到towncount = 1?

    private static Cars[] Read(string file, Town[] town, int townCount, Dictionary<string, Cars>cars)
    {
        Cars[] Cars = new Cars[MaxNumberOfCars];
        using (StreamReader reader = new StreamReader(@file))
        {
            string line = null;

            string Dealership = reader.ReadLine();
            Console.WriteLine("city {0}", Dealership);
            string adress = reader.ReadLine();
            Console.WriteLine("adress {0}", adress);
            string phonenumber = reader.ReadLine();
            Console.WriteLine("phone {0}", phonenumber);
            townCount++;
            Console.WriteLine("towncount {0}", townCount);
            town[townCount] = new Town(Dealership);

            while ((line = reader.ReadLine()) !=null)
            {
                string[] values = line.Split(';');
                string licenseplates = values[0];
                Console.WriteLine("number {0}", licenseplates);
                string brand = values[1];
                Console.WriteLine("brand {0}", brand);
                string model = values[2];
                Console.WriteLine("model {0}", model);
                DateTime yearofmake = DateTime.Parse(values[3]);
                Console.WriteLine("year of make {0}", yearofmake);
                DateTime now = DateTime.Today;  
                int age = now.Year - yearofmake.Year;
                DateTime techinspection = DateTime.Parse(values[4]);
                Console.WriteLine("tech inspection {0}", techinspection);
                string fuel = values[5];
                Console.WriteLine("fuel type {0}", fuel);
                int fuelconsumption = int.Parse(values[6]);
                Console.WriteLine("consumption {0}", fuelconsumption);
                town[townCount].DealershipCount++;
                Cars car = new Cars(licenseplates, brand, model, yearofmake, techinspection, fuel, fuelconsumption);
                Cars[carCount++] = car;
                Console.WriteLine("deal count {0}", town[townCount].DealershipCount);

            }
            return Cars;
        }

    }

1 个答案:

答案 0 :(得分:2)

如果您与参数townCount相关,那么答案很简单。默认情况下,参数是按值给出的,这意味着您确实传递了towncount的值而不是对它的引用。你应该用这个:

 private static Cars[] Read(string file, Town[] town, ref int townCount, Dictionary<string, Cars>cars)

请参阅此link on MSDN for further information on paramaters