Question

The main problem is that I have already created and populated the model, so I don't want to create a new model. The Query method uses the model to do it's comparisons, so if I pass it in, I need to find a way to reference it when I call the method.

However if I declare it as a reference variable inside the switch method, then call it, I get an error "use of the unassigned local variable 'UModel' "

And if I declare it outside the method, I get an issue "object reference required for non-static field"

Can you tell me what's going on and possibly how to fix it?

Here is my Controller, where I create the model and pass in the OP variable:

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

Here is my Façade:

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

Here is the Model:

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;
    }
}
Was it helpful?

Solution

Solution 1

declare UModel as a static variable (outside the methods) like:

public static User_Model UModel;

and change

User_Model UModel = new User_Model();

in CompareLogin to

UModel = new User_Model();

and remove

User_Model UModel;

from Operation_Switch


Solution 2

Pass UModel to Operation_Switch as a parameter

by changing

public static void Operation_Switch(int OP)

to

public static void Operation_Switch(int OP, User_Model UModel)

removing

User_Model UModel;

from Operation_Switch and changing

Database_Facade.Operation_Switch(OPREAD);

in CompareLogin to

Database_Facade.Operation_Switch(OPREAD, UModel);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top