簡體   English   中英

C#類列表和屬性

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM