2011-07-04 28 views
0
public bool SetupEmpty(UserViewModel model, SimsContext db) 
{ 
    if (model != null && db != null) 
    { 
     // Setup the User 
     model.User = new T2.Models.User(); 
     model.User.Roles = ""; 
     model.User.ActiveUser = true; 

    } 
    return false; 
} 
+0

好吧,我們沒有關於這個問題的「上下文」,所以我們可以推斷出你的代碼正在檢查一個對象是否存在。 – RPM1984

回答

1

檢查在開始使用model和db之前,它們都已設置。否則,如果其中一個沒有設置,程序可能會崩潰。

+0

所以你的意思是userviewmodel model = new userviewmodel; – Lalit

+0

是的,model和db都必須實例化。如果model爲null,並且我們嘗試執行model.User,我們將得到一個NullReferenceException。訪問不存在的對象的屬性是沒有意義的。在旁註中,db變量從未被實際使用過,這有點令人困惑。 –