2016-09-23 25 views
0

我有一个名为第一类:不能值传送到另一个类(C#)

  Appartments[] appartments; 

我想复制“数”的值,并将其粘贴到“AppartmentInfo.cs”级(我猜他们被称为参数)。

 public static void FindFloor(Appartments[] appartments, int amount,out int floorToRound,out AppartmentInfo[] AInfo) 
    { 
     floorToRound = 0; 
     AInfo = new AppartmentInfo[Max]; 
     for (int i = 0; i < amount; i++) 
     { 
      AInfo[i].Floor1 = Convert.ToDouble(appartments[i].Number); 
      Console.WriteLine(appartments[i].Number); 
      if (appartments[i].Number < 27) 
      { 
       appartments[i].Number = 1; 
      } 
      else 
      { 

       appartments[i].Number /= 27; 
      } 
     } 
    } 

如果有需要我可以发布整个代码。我收到错误: NullReferenceException未处理。

错误就行了:

AInfo[i].Floor1 = Convert.ToDouble(appartments[i].Number); 

全码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 

namespace P3._1 
{ 
class Program 
{ 
    public const int Max = 20; 
    static void Main(string[] args) 
    { 
     int amount, AmountOfRooms, price, floor, floorToRound; 
     Appartments[] appartments; 
     AppartmentInfo[] AInfo; 
     //AppartmentInfo AInfo = new AppartmentInfo(); 
     ReadData(out appartments, out amount); 
     TextImput(out floor, out AmountOfRooms, out price); 
     FindFloor(appartments, amount,out floorToRound,out AInfo); 
     Console.ReadKey(); 
    } 
    public static void ReadData(out Appartments[] appartments, out int amount) 
    { 
     amount = 0; 
     appartments = new Appartments[Max]; 
     using (StreamReader reader = new StreamReader("C:\\Users\\Andrius\\Desktop\\Mokslams\\C#\\Pratybos\\P3\\P3.1 be konstruktoriaus\\Appartments.csv", true)) 
     { 
      reader.ReadLine(); 
      string line = null; 
      while (null != (line = reader.ReadLine())) 
      { 
       string[] values = line.Split(','); 
       int number = int.Parse(values[0]); 
       int area = int.Parse(values[1]); 
       int rooms = int.Parse(values[2]); 
       int sellingCost = int.Parse(values[3]); 
       int phone = int.Parse(values[4]); 

       Appartments appartmentsObj = new Appartments(number, area, rooms, sellingCost, phone); 
       appartments[amount++] = appartmentsObj; 
      } 
     } 
    } 

    public static void TextImput(out int floor, out int AmountOfRooms, out int price) 
    { 
     AmountOfRooms = 0; 
     price = 0; 
     Console.WriteLine("Iveskite buto aukštą: "); 
     floor = int.Parse(Console.ReadLine()); 
     Console.WriteLine("Iveskite kambariu skaiciu: "); 
     AmountOfRooms = int.Parse(Console.ReadLine()); 
     Console.WriteLine("Maksimali kaina: "); 
     price = int.Parse(Console.ReadLine()); 


    } 

    public static void FindFloor(Appartments[] appartments, int amount,out int floorToRound,out AppartmentInfo[] AInfo) 
    { 
     floorToRound = 0; 
     AInfo = new AppartmentInfo[Max]; 
     for (int i = 0; i < amount; i++) 
     { 
      AInfo[i].Floor1 = Convert.ToDouble(appartments[i].Number); 
      Console.WriteLine(appartments[i].Number); 
      if (appartments[i].Number < 27) 
      { 
       appartments[i].Number = 1; 
      } 
      else 
      { 

       appartments[i].Number /= 27; 
      } 
     } 
    } 




} 

}

回答

0

你从零循环迭代达 - 1,然后尝试以查找在该数组的Appartments数组中输入数组。

传递给金额字段的值可能大于套房数组的大小。

很难说如果不知道公寓数组是什么样的,那么amount参数中的值是多少。

+0

从我的观点,量从未通过边界。我刚刚上传完整代码 – Andrius

+0

它在for循环的第一次迭代中失败吗? – Banners

+0

我问,因为我跟Bens回答的描述一致。他说得对。您的数组已初始化,但其中的元素不是。 – Banners

1

您试图访问AInfo[i].Floor1财产,但如果AppartmentInfo是一个类,然后在你的AInfo阵列中的每个元素将是null。您需要先创建一个AppartmentInfo对象:

for (int i = 0; i < amount; i++) 
    { 
     AInfo[i] = new AppartmentInfo 
     { 
      Floor1 = Convert.ToDouble(appartments[i].Number) 
     }; 

编辑/更新:完成/编译程序来演示语法:

public class Program 
{  
    public static void Main() 
    { 
     var meow = new Foo[3]; 

     for (int i = 0; i < 3; ++i) 
     { 
      meow[i] = new Foo 
      { 
       Bar = Convert.ToDouble("3.141"), 
       Blah = "Another Test", 
      };    

      if (i < 2) 
      { 
       meow[i].Buzz = 3; 
      } 
     } 
    } 

    class Foo 
    { 
     public double Bar { get; set; } 
     public string Blah { get; set; } 
     public int Buzz { get; set; } 
    } 
}  
+0

非常感谢,但我还有一个问题。有没有更简单的方法来创建一个AppartmentInfo对象?例如,我不仅需要传送“Floor1”,还需要传送“房间”,“价格”等许多其他内容。我还需要检查要传送的内容和不需要的内容,因此我需要使用IF,但是我无法使用IF内部的AInfo [i] = new AppartmentInfo {} this。 – Andrius

+0

@Andrius您可以使用逗号''''在'{}'中设置多个属性来分隔每个属性,如果需要'if'检查,那么你仍然可以在创建对象后设置属性。 –