【问题标题】:How do I pass an object?我如何传递一个对象?
【发布时间】: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


    【解决方案1】:

    解决方案 1

    将 UModel 声明为静态变量(在方法之外),例如:

    public static User_Model UModel;
    

    改变

    User_Model UModel = new User_Model();
    

    在比较登录到

    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);
    

    在比较登录到

    Database_Facade.Operation_Switch(OPREAD, UModel);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-28
      • 2021-08-07
      • 2021-09-23
      • 2012-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多