【发布时间】:2014-03-04 09:47:13
【问题描述】:
主要问题是我已经创建并填充了模型,所以我不想创建新模型。 Query 方法使用模型进行比较,所以如果我传入它,我需要在调用该方法时找到一种方法来引用它。
但是,如果我在 switch 方法中将其声明为引用变量,然后调用它,则会出现错误“使用未分配的局部变量 'UModel'”
如果我在方法之外声明它,我会收到一个问题“非静态字段需要对象引用”
你能告诉我发生了什么以及如何解决吗?
这是我的控制器,我在其中创建模型并传入 OP 变量:
public static void CompareLogin(User_LoginView Login_View)
{
// Creates a new oject of User_Model and populates it from the User_Controller.
User_Model UModel = new User_Model();
UModel.Name = screenName;
UModel.Pwd = screenPwd;
// Runs the Login Comparsion in the Database_Facade, and passes in the Read Operation variable.
new Database_Facade();
Database_Facade.Operation_Switch(OPREAD);
}
这是我的外观:
public static void Operation_Switch(int OP)
{
Database_MySQLQueryFunctions SqlQuery;
User_Model UModel;
PAC_Model PModel;
Cable_Model CModel;
switch (OP)
{
case 101:
SqlQuery = new Database_MySQLQueryFunctions();
SqlQuery.GetLoginAccess(UModel);
// Repopulates the Model to return the Access Level.
break;
case 102:
SqlQuery = new Database_MySQLQueryFunctions();
SqlQuery.SetLoginAccess(PModel);
break;
case 103:
SqlQuery = new Database_MySQLQueryFunctions();
SqlQuery.UpdateUser(CModel);
break;
}
这是模型:
public class User_Model
{
public string Name { get; set; }
public string Pwd { get; set; }
public string ConfirmPwd { get; set; }
public int AccessLevel { get; set; }
public User_Model()
{
Name = null;
Pwd = null;
ConfirmPwd = null;
AccessLevel = 0;
}
}
【问题讨论】:
标签: c# parameter-passing pass-by-reference