简体   繁体   English

如何封装具有类属性的列表?

[英]How to encapsulate list that has class property?

How to encapsulate below codes (i need refoctoring) 如何封装以下代码(我需要反演)

    public class mycollection
{
        private DateTime tarih;
        private int sira;
        private int visitingcount;

        public DateTime TARIH { get { return tarih; } set { tarih = value; } }
        public int SIRA { get { return sira; } set { sira = value; } }
        public int VISITINGCOUNT { get { return visitingcount; } set { visitingcount = value; } }
}

i used this class below 我在下面用这个课

DataRow[] rows = dsChart.Tables[0].Select("TARIH>='" + DateGap1 + "' and TARIH<='" + DateGap2 + "'");

                                list = new List<mycollection>();
                                foreach (DataRow dr in rows)
                                {
                                    mycollection mycol = new mycollection();
                                    mycol.TARIH = Convert.ToDateTime(dr["TARIH"].ToString());
                                    mycol.SIRA = Convert.ToInt32(dr["SIRA"].ToString());
                                    mycol.VISITINGCOUNT = Convert.ToInt32(dr["VISITINGCOUNT"].ToString());
                                    list.Add(mycol);
                                }

i need like that : 我需要这样:

  public static void LoadDepartman(string departmanName)
        {
            List<StuffDepartman> li = new List<StuffDepartman>();
            GetDepartman departman = new GetDepartman();
            DataSet ds = departman.GetDepartmanA(departmanName);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                li.Add(new StuffDepartman
                {
                    Name = dr["Name"].ToString(),
                    SurName = dr["SurName"].ToString(),
                    Address = dr["Address"].ToString(),
                    Phone = dr["Phone"].ToString(),
                    DepartmanName = dr["DepartmanName"].ToString(),
                    Salary =int.Parse( dr["Salary"].ToString()),
                    Married = dr["Married"].ToString()
                }
                );
            }
            HttpContext.Current.Session["Stuffs"] = li;

        }

(I don't fully understand how the first two examples relate to the third... they don't seem to share anything in common) (我不完全理解前两个示例与第三个示例的关系……它们似乎没有任何共同点)

So what currently breaks? 那么当前发生了什么? The biggest issue I can see is that (IIRC) to be reliably usable with Session (with different back end implementations) the item must be serializable (var BinaryFormatter ), but that may be as simple as marking mycollection with [Serializable] . 我看到的最大问题是(IIRC)要与Session 可靠地使用(具有不同的后端实现),该项目必须可序列化(var BinaryFormatter ),但这可能很简单, mycollection[Serializable]标记mycollection一样简单。

For info mycollection is a confusing name for something that is a row entity , not the actual collection - and if you have C# 3.0 you could simplify things a bit with automattically implemented properties: 对于信息, mycollection行实体而不是实际集合的名称的一个令人困惑的名称-如果您具有C#3.0,则可以使用自动实现的属性来简化一些操作:

[Serializable]
public class SomeSensibleName
{
    public DateTime Tarih {get;set;}
    public int Sira {get;set;}
    public int VisitCount {get;set;}
}

However; 然而; it isn't clear (from your code) whether this will change anything. 从您的代码尚不清楚这是否会改变任何东西。 Please can you clarify what is currently happening? 请您能说明目前发生了什么吗?

If I understand correctly, this is the refactoring you are looking for: 如果我正确理解,这就是您要寻找的重构:

DataRow[] rows = dsChart.Tables[0].Select("TARIH >='" + DateGap1 + "' and TARIH <='" + DateGap2 + "'");
List<mycollection> list = new List<mycollection>();
foreach (DataRow dr in rows)
{
    list.Add(new mycollection
    {   
      TARIH = Convert.ToDateTime(dr["TARIH"].ToString());
      SIRA = Convert.ToInt32(dr["SIRA"].ToString());
      VISITINGCOUNT = Convert.ToInt32(dr["VISITINGCOUNT"].ToString());
    });
}

Keep in mind that in order for this to work you need to be running the .NET Framework 3.x (VS2008). 请记住,为了使其正常工作,您需要运行.NET Framework 3.x(VS2008)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM