我有一类房子(这里没有显示频道枚举)
该类具有引用的对象和两个构造函数,然后在类中有一个名为“inventory”的List和两个或多个尝试访问列表的函数,我想要做的是使用相同的列表“班级房屋”内的所有独立功能 但我得到一个'当前上下文中不存在'的错误,这让我觉得这是一个范围问题。 但是所有的方法都在同一个“班级房屋”......它们的范围不同吗?
显然没有走正确的道路,我该如何看待这个?
class property
{
public string AddressNumber { get; set; }
public string AddressStreet { get; set; }
public double ListingPrice { get; set; }
public double MonthlyRent { get; set; }
public double Taxes { get; set; }
public Channel Channel { get; set; }
public property(string addnum, string addstreet, double lp, double mr, double taxes, Channel rorm)
{
AddressNumber = addnum;
AddressStreet = addstreet;
ListingPrice = lp;
MonthlyRent = mr;
Taxes = taxes;
Channel = rorm;
}
public property()
{
AddressNumber = "4319";
AddressStreet = "Forestview";
ListingPrice = 600000;
MonthlyRent = 0;
Taxes = 2000;
Channel = Channel.Rosa;
}
static List<property> inventory = new List<property>
{
new property("19263","Collingham",24000,700,1744, Channel.Rosa),
new property("16003","Collingham",24000,700,1672, Channel.Rosa),
new property("13051","Bringard",24000,700,1305, Channel.Rosa),
new property("10409","Bringard",24000,650,1591, Channel.Rosa),
new property("10086","Marne",24000,650,1176, Channel.Rosa),
new property("10042","Peerless",24000,650,1313, Channel.Rosa),
new property("10736","Steel",30000,1000,1587, Channel.Mike),
new property("2010","Glendale",30000,1000,1587, Channel.Mike),
new property("2260","Blain",30000,1000,1587, Channel.Mike),
new property("3000","Fullerton",30000,1000,1587, Channel.Mike),
};
property addtolist= new property("864", "Alter", 30000, 650, 1600, Channel.Rosa);
inventory.Add(addtolist);
public static void userAddItem()
{
//I want to use the list here
}
public static void allItemsToFile()
{
//and here
}
} //end of house class bracket
答案 0 :(得分:0)
您应该将您的类视为对象。所以你的家庭班可以只是一个定义房子的对象。
public class houses
{
public string AddressNumber { get; set; }
public string AddressStreet { get; set; }
public double ListingPrice { get; set; }
public double MonthlyRent { get; set; }
public double Taxes { get; set; }
public Channel Channel { get; set; }
}
现在,您想要列出属性。在你的主要课程中,你可以做这样的事情;
private List<houses> properties = new List<houses>();
houses house1 = new houses();
house1.AddressNumber = "19263";
house1.AddressStreet = "Collingham";
house1.ListingPrice = 24000.0;
hosue1.MonthlyRent = 700.0;
house1.Taxes = 1744.0;
house1.Channel = Channel.Rosa;
properties.Add(house1);
现在您有一个属性列表,您可以根据需要访问它们。
Foreach(houses h in properties)
{
Console.WriteLine("{0},{1},{2},{3},{4},{5}", h.AddressNumber, h.AddressStreet, h.ListingPrice, h.MonthlyRent, h.Taxes, h.Channel.toString());
}