C#类列表和属性

时间:2013-05-17 11:43:12

标签: c# list class

我对编程非常陌生,所以这对你们大多数人来说可能是一个愚蠢的问题,但是我仍然试图在谷歌看一下这里:

我创建了一个包含一些属性和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,但它们仍然出现在列表中:(

我做错了什么?

提前感谢并抱歉这个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;
        }

5 个答案:

答案 0 :(得分:3)

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

您的属性:

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

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

 List<orders> getOrders()

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

此外,如果您不希望显示所有订单属性,那么您 可以创建几个类并从中继承。

请注意,您正在重新调整具有所有属性的订单列表 设置为公开,所以他们总是会出现,尽管他们没有 被初始化或有价值。

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

答案 1 :(得分: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; }
}

答案 2 :(得分:1)

getOrders方法返回的是订单列表。 orders类具有属性year和totalYear,即使您没有设置它们,在实例化此类的实例时也会分配它们所需的空间。

BTW,我建议您使用Pascal Casing作为类型名称,并使用单数名词。 (即订单而不是订单)。

答案 3 :(得分: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
 } 
}

答案 4 :(得分: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; }
我们呢?并不是的 现在你有多个选择

  • 创建一个包含此属性的单独类
  • 使用anonymous Type
  • 使用Dictionary
  • 使用DataTable

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