0

我对编程很陌生,所以这对你们大多数人来说可能是一个愚蠢的问题,但由于我已经尝试在 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,但它们仍然出现在列表中:(

我究竟做错了什么 ?

在此先感谢并抱歉这个菜鸟问题。

更新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;
        }
4

5 回答 5

3

我不太明白你的意思,但是。

你的属性:

public string totalYear { get; set; }
public string year { get; set; }

设置为公开,因此它们出现在以下列表中:

 List<orders> getOrders()

如果您不希望它们出现,只需将它们设为私有即可。

此外,如果您不想显示所有 Orders 属性,您可以创建多个类并从它们继承。

考虑一下,您正在重新调整所有属性都设置为公共的订单列表,因此它们将始终出现,尽管它们没有被初始化或具有值。

您应该创建两个不同的类。一个用于订单,另一个用于订单日期,如果您不希望它们始终出现。

于 2013-05-17T11:45:37.177 回答
3

考虑到您正试图将订单类视为某种发票系统或购物车,我将为此设置两个类。

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; }
}
于 2013-05-17T12:38:03.317 回答
1

您可以将班级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
 } 
}
于 2013-05-17T12:05:17.507 回答
1

getOrders 方法返回的是订单列表。orders 类有属性 year 和 totalYear 即使你没有设置它们,当你实例化这个类的一个实例时,它们所需的空间就会被分配。

顺便说一句,我建议您对类型名称使用Pascal Casing,并为它们使用单​​数名词。(即,订购而不是订购)。

于 2013-05-17T11:54:46.470 回答
1

你的问题不是你的代码你的问题是你认为你在做什么:)
你编写了 OO 代码,所以你应该了解 OO 是什么以及它是如何工作的

但是现在对于您的“问题”,请确保您会看到您的属性,因为它们都是public如此,让我们根据您的代码深入研究 OO

让我们从你的班级名称开始orders

应该是Order因为这个类型的每个对象都是一个订单。

现在让我们看看哪些属性只与订单相关

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 因为我在您提供的代码中看不到限制,因此您的列表将正确包含数据库中的所有订单,否则您应该将限制添加到您的方法名称(示例getOpenOrders()

好的,现在你的第二种方法

现在它来了,我们需要 2 个属性!?!

public string totalYear { get; set; }
public string year { get; set; }

我们要不要?不是现在你有多种选择

  • 创建一个包含此属性的单独类
  • 使用匿名类型
  • 用一个Dictionary
  • 用一个DataTable

现在是你自己的决定...

于 2013-05-17T12:37:10.207 回答