如題,我在一個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"];
        }
應該如何寫,請路過的大俠指點,謝謝!

解决方案 »

  1.   

    重构需要不同数量或者类型的参数,两个相同类型的参数没法重构。
    不过你可以尝试通过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[]这样的方式,不是很好的结构。
      

  2.   

    用构造函数啊,public InvoiceOrder(DataRow dr,bool f)
    第一个加上这个函数 根据传的参数不同调用
    我不明白你是想重写daterow的颜色还是这样以上说的
      

  3.   

    TO:wood001 (小夜小鹰)引你說的 "不过说实话,楼主这样使用DataRow很危险,在DataRow内部数据不可知的情况下, 
    使用(string)dr[]这样的方式,不是很好的结构。"
    我想請教下一般用什麼樣的結構來寫才更安全??(謝謝)
      

  4.   

    为了获得类型的安全,需要牺牲一些效率。像你写的这个类,传入的参数DataRow中,必须保证每个数据项都包含基本的值,即不能出现DBNull的情况。
    一种解决方法是使DataRow包含默认值;
    第二种就是使用时判定不出现DBNull,if (dr["???"] == DBNull.Value)