我很新的编程所以这可能是对你最愚蠢的问题,但还是因为我已经尝试在谷歌看看,这里有云:C#类目录和属性
我创建了一个具有一些属性和2个方法的类,其中一些属性应该是第一个方法,另一个属性是第二个方法。
这两种方法都会返回一个列表,我的问题是这两个列表都会返回所有的属性。不知道为什么,因为我'不使用所有的人在这两种方法......这里是一个例子:
class orders
{
public string invoiceID { get; set; }
public string employee { get; set; }
public string store { get; set; }
public string client { get; set; }
public string invoiceDate { get; set; }
public string total { get; set; }
public string totalYear { get; set; }
public string year { get; set; }
public static List<orders> getOrders()
{
string sSQL = "bla bla bla huge query "
DBConnect connect = new DBConnect();
DataTable dt = new DataTable();
dt = connect.getBD(sSQL);
List<orders> o = new List<orders>();
for (int i = 0; i < dt.Rows.Count; i++)
{
orders ord = new orders();
ord.invoiceID = dt.Rows[i]["invoiceid"].ToString();
ord.employee = dt.Rows[i]["empname"].ToString();
ord.store = dt.Rows[i]["storename"].ToString();
ord.client = dt.Rows[i]["clientname"].ToString();
ord.invoiceDate = ((DateTime)dt.Rows[i]["invoicedate"]).ToString("dd-MM-yyyy");
ord.total = dt.Rows[i]["total"].ToString();
o.Add(ord);
}
return o;
所以在这个方法我没有使用的公共属性一年totalYear,但他们仍然出现在列表:(
我在做什么错?
预先感谢和抱歉noob问题。
更新1(第二种方法)
public static List<orders> getTotalYearInvoices()
{
DateTime date = DateTime.Now;
int year = date.Year;
List<orders> o = new List<orders>();
for (int i = 2009; i < year; i++)
{
string sSQL = " another huge query"
DBConnect connect = new DBConnect();
DataTable dt = new DataTable();
dt = connect.getBD(sSQL);
orders ord = new orders();
ord.year = i.ToString();
for (int j = 0; j < dt.Rows.Count; j++)
{
ord.totalYear = dt.Rows[j]["total"].ToString();
}
o.Add(ord);
}
return o;
}
你能否确认你发布了该课程的所有代码?我只在你的班级看到一种方法。 – Matthew
@Matthew在这种情况下,它并不重要,因为问题是他对OOP的理解 – WiiMaxx
@Mthethew我只发布了一种方法是的,我认为这是足够的,因为第一种方法没有设置所有的属性,我仍然把它们全部列入清单。 – n3bi0s