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