如題,我在一個classA中已定義了如下dr public InvoiceOrder(DataRow dr)
{
this.orderId = (string)dr["order_id"];
this.needRecept = (bool)dr["need_recept"];
this.status = (string)dr["status"];
this.paid = (decimal)dr["amount"];
this.re = (string)dr["re"];
}現在我想在同一個classA中重定義dr(注意紅色code的區別)
public InvoiceOrder(DataRow dr)
{
this.orderId = (string)dr["order_id"];
this.needRecept = (bool)dr["need_recept"];
this.paid = (decimal)dr["amount"];
this.re = (string)dr["re"];
}
應該如何寫,請路過的大俠指點,謝謝!
{
this.orderId = (string)dr["order_id"];
this.needRecept = (bool)dr["need_recept"];
this.status = (string)dr["status"];
this.paid = (decimal)dr["amount"];
this.re = (string)dr["re"];
}現在我想在同一個classA中重定義dr(注意紅色code的區別)
public InvoiceOrder(DataRow dr)
{
this.orderId = (string)dr["order_id"];
this.needRecept = (bool)dr["need_recept"];
this.paid = (decimal)dr["amount"];
this.re = (string)dr["re"];
}
應該如何寫,請路過的大俠指點,謝謝!
不过你可以尝试通过Flag参数,实现上述需求。 public InvoiceOrder(DataRow dr,int iFlag)
{
switch (iFlag){
case 0:
this.orderId = (string)dr["order_id"];
this.needRecept = (bool)dr["need_recept"];
this.status = (string)dr["status"];
this.paid = (decimal)dr["amount"];
this.re = (string)dr["re"];
break;
case 1:
this.orderId = (string)dr["order_id"];
this.needRecept = (bool)dr["need_recept"];
this.paid = (decimal)dr["amount"];
this.re = (string)dr["re"];
break;
}
}
不过说实话,楼主这样使用DataRow很危险,在DataRow内部数据不可知的情况下,
使用(string)dr[]这样的方式,不是很好的结构。
第一个加上这个函数 根据传的参数不同调用
我不明白你是想重写daterow的颜色还是这样以上说的
使用(string)dr[]这样的方式,不是很好的结构。"
我想請教下一般用什麼樣的結構來寫才更安全??(謝謝)
一种解决方法是使DataRow包含默认值;
第二种就是使用时判定不出现DBNull,if (dr["???"] == DBNull.Value)