Come incapsulare lista che ha proprietà di classe?
https://stackoverflow.com/questions/931649
Domanda
Come incapsulare sotto codici (ho bisogno 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; } }
}
ho usato questa classe seguente
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);
}
ho bisogno di simile:
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;
}
Nessuna soluzione corretta
Altri suggerimenti
(non capisco appieno come i primi due esempi si riferiscono al terzo ... non sembrano condividere qualcosa in comune)
Allora, cosa attualmente si rompe? Il problema più grande che posso vedere è che (IIRC) per essere in modo affidabile utilizzabile con Session
(con diverse implementazioni di back-end) l'articolo deve essere serializzabile (var BinaryFormatter
), ma che può essere semplice come mycollection
marcatura con [Serializable]
.
Per info mycollection
è un nome di confusione per qualcosa che è un fila entità , non la collezione reale - e se si dispone di C # 3.0 si potrebbe semplificare le cose un po 'con proprietà automattically implementate:
[Serializable]
public class SomeSensibleName
{
public DateTime Tarih {get;set;}
public int Sira {get;set;}
public int VisitCount {get;set;}
}
Tuttavia; non è chiaro (dal codice) se questo cambierà nulla. Si prega si può chiarire che cosa sta accadendo?
Se ho capito bene, questo è il refactoring che stai cercando:
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());
});
}
Tieni presente che, al fine di far funzionare tutto questo è necessario essere in esecuzione il .NET Framework 3.x (VS2008).