2016-01-06 39 views
0

我嘗試生成16位代碼,當我添加記錄時,所有編號都是相同的,我該如何修復它?生成代碼和更新列表中的錯誤

in loop befor code [i] = Newcode;當代碼添加到列表中時,代碼是corect將最後一個代碼更改爲new,因此所有代碼都與最後生成的相同!

public ActionResult Gen5Rm(GenerateCodeModel model){ 

     Code Newcode = new Code(); 

     int X = Convert.ToInt32(model.Quntity); 

     Code[] code = new Code[X]; 

     for (int i = 0; i < X; i++) 
     {  
       string strDate = ""; 
       string strmonth = ""; 
       string strday = ""; 
       string myday = ""; 

       strmonth = DateTime.Now.ToString("MM"); 
       myday = DateTime.Now.DayOfWeek.ToString(); 


       if (myday == "Sunday") 
       { 
        strday = "SU"; 
       } 
       if (myday == "Monday") 
       { 
        strday = "MO"; 
       } 
       if (myday == "Tuesday") 
       { 
        strday = "TU"; 
       } 
       if (myday == "Wednesday") 
       { 
        strday = "WE"; 
       } 
       if (myday == "Thursday") 
       { 
        strday = "TU"; 
       } 
       if (myday == "Friday") 
       { 
        strday = "FR"; 
       } 
       if (myday == "Saturday") 
       { 
        strday = "ST"; 
       } 
       strDate = generateBarcode(); 
       Newcode.codebase = model.Agent + strmonth + 
      model.Type_Code + strDate + strday + "5" + model.Dealercode; 
       Newcode.price = 5; 
       Newcode.serial = "S" + DateTime.Now.ToString("mmssfff"); 

       code[i] = Newcode; 
     } 

     return PartialView("_return5rm", code); 
     } 

the export is like this

回答

0

Newcode應該是內部的for循環中,否則該陣列中的所有項將指向相同的對象(同一存儲器地址)

for (int i = 0; i < X; i++) 
{ 
    Code Newcode = new Code(); 
    ... 
}