2015-10-16 52 views
-4

为什么我一直得到towncount = 1?C#++函数不起作用。 Int stays = 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; 
     } 

    } 
+0

也许是'0'?如果您的代码中出现意外结果,请告诉您期望的值以及首先。这样很难回答。 –

回答

2

如果你涉及到参数townCount那么答案很简单。参数默认值为默认值,意味着您通过的值为towncount而不是对其的引用。你应该使用这个:

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

见_that_线在此之前link on MSDN for further information on paramaters