[英]C# Class list and properties
我对编程非常陌生,因此对于大多数人来说这可能是一个愚蠢的问题,但是由于我已经尝试过在Google中进行查找,因此,问题来了:
我创建了一个具有一些属性和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;
因此,在这种方法中,我没有使用公共属性year和totalYear,但它们仍显示在列表中:(
我究竟做错了什么 ?
在此先感谢您,并对菜鸟的问题表示抱歉。
UPDATE 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;
}
我不是很明白你的意思,但是。
您的属性:
public string totalYear { get; set; }
public string year { get; set; }
设为公开,因此它们出现在以下列表中:
List<orders> getOrders()
如果您不希望它们出现,只需将它们设为私有。
另外,如果您不希望显示所有的Orders属性,则可以创建几个类并从中继承。
认为,您正在重新调整所有属性都设置为public的订单列表,因此尽管它们尚未初始化或没有值,但它们将始终出现。
您应该创建两个不同的类。 如果您不希望它们始终显示,则一个用于订单,另一个用于订单日期。
考虑到您正在尝试将订单类视为某种发票系统或购物车,我将有两个类别。
public class InvoiceSystem
{
private List<Invoice> currentOrders;
public InvoiceSystem()
{
currentOrders = new List<Invoice>();
}
public void Populate()
{
//fill your list from the database
}
public void Save()
{
//Save list back to database
}
public List<Invoice> GetInvoices(/*perhaps something to filter */ )
{
// return filtered list, or entire list.
}
public decimal GetTotalForYear(int year)
{
// iterate through list (or use linq) to get total and return it
}
}
public class Invoice
{
public int InvoiceID { get; set; }
public string Employee { get; set; }
public string Store { get; set; }
public string Client { get; set; }
public DateTime InvoiceDate { get; set; }
public decimal Total { get; set; }
}
getOrders方法返回的是订单列表。 order类具有属性year和totalYear,即使您未设置它们,在实例化此类的实例时也会为其分配所需的空间。
顺便说一句,我建议您将Pascal Casing用于类型名称,并将其使用单数名词。 (即订购而不是订购)。
您可以将班级orders
分为两个班级:(是否有您没有的原因?)
public class BaseOrders
{
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 static List<BaseOrders> getOrders()
{
//your implementation
}
}
然后你可以约会
public class DateOrders
{
public string totalYear { get; set; }
public string year { get; set; }
public static List<DateOrders> getTotalYearInvoices()
{
//your implementation
}
}
您的问题不是您的代码,您的问题是您认为自己在做的事情:)
您编写OO代码,因此您应该了解什么是OO及其工作方式
但是现在到您的“问题”,确保您将看到您的属性,因为它们都是public
因此让我们根据您的代码来挖掘面向对象
让我们从您的班级名称orders
开始
它应该是Order
因为此Type的每个对象都是一个订单。
现在让我们看看哪些属性仅与订单相关
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 static List<orders> getOrders()
我将其重命名为public static List<Order> getAllOrders()
getAll,因为我在您提供的代码中看不到限制,因此您的列表将正确包含数据库中的所有Order,否则您应将限制添加到您的方法名称中(示例getOpenOrders() )
好吧,第二种方法
现在我们需要2个属性!?!
public string totalYear { get; set; }
public string year { get; set; }
我们要不要? 现在不是,您有多种选择
Dictionary
DataTable
这是您自己的决定...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.