简体   繁体   中英

C# Login with rights

I need to make an application for school that includes a login and rights. I am checking the login and am trying to store the userlevel in a class. I tried using a global variable, but somehow it doesn't work.

EDIT: This problem is semi-solved. Opening the form isn't. (Read the last part)

Class (userlevel):

private static int ulevel = 99;

public static int gCheckLevel
{
    get { return ulevel; }
    set { ulevel = value; }
}

When logged in this will happen:

userlevel.gCheckLevel = ulvl;
Main_MDI main = new Main_MDI();
main.mCommitRights();

Trying to open the right form after the login is succesful

Form start_screen_admin = new start_screen_admin();

public void mCommitRights()
{
    if (userlevel.gCheckLevel == 0)
    {
        // Admin, no changes
        MessageBox.Show("Admin");
        mForms(start_screen_admin);
    }
}

But the last part doesn't work. What am I doing wrong here? I need to store the userlevel, because querying to the database every single time I need it is not necessary.

EDIT:

The last part doesn't work 100%, the global works, but the mForms doesn't, but it does anywhere else.

private void mForms(Form f)
{
    if (this.MdiChildren.Contains(f)) { f.WindowState = FormWindowState.Normal; }
    else { f.MdiParent = this; f.Show(); }
}

Things you can check:

  • Is the userLevel class defined as public static ?
  • Does the variable ulvl , when the user has logged in, have the correct value (0, in this case): try to set userlevel.gCheckLevel = 0; directly and see if it works. If it does, then your problem is with the ulvl variable.

Can you access the static property from an instance of the class like that? Why are they static? Can they not just be instance properties?

MSDN says "A static method, field, property, or event is callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member. Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events. Static members are often used to represent data or calculations that do not change in response to object state; for instance, a math library might contain static methods for calculating sine and cosine"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM