主要的问题是我已经创建并填充了模型,所以我不想创建一个新的模型。查询方法使用模型进行比较,因此如果传递它,则需要在调用该方法时找到引用它的方法。
但是,如果我在开关方法中声明它为一个引用变量,然后调用它,我将得到一个错误“使用未分配的局部变量'UModel‘”。
如果我在方法之外声明它,就会得到一个题为“非静态字段所需的对象引用”。
,你能告诉我发生了什么事,可能是怎么解决的吗?
下面是我的Controller,在这里我创建模型并传入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;
}
}发布于 2014-02-07 07:27:34
解决方案1
将UModel声明为静态变量(方法外)如下:
public static User_Model UModel;和改变
User_Model UModel = new User_Model();在CompareLogin中
UModel = new User_Model();移除
User_Model UModel;来自Operation_Switch
解决方案2
将UModel作为参数传递给Operation_Switch
通过改变
public static void Operation_Switch(int OP)至
public static void Operation_Switch(int OP, User_Model UModel)删除
User_Model UModel;从Operation_Switch到变迁
Database_Facade.Operation_Switch(OPREAD);在CompareLogin中
Database_Facade.Operation_Switch(OPREAD, UModel);https://stackoverflow.com/questions/21620005
复制相似问题