Question

i am trying to implement a windows desktop applicaiton c# VS 2010. To Application has a login form to ask the user to type his username and password. and the applicaiton checks them in the DB and returns the userdata (id, permissions, email, telephone) which are saved in DB.

Programmatically, when the login data is valid and correct, i create an instant from a class classed "clsUser" which provides the fields for the users. and then fill the class with the data as normal and then open the main form and child forms after that. My question is , how to access the class of user over the whole applicaiton (for example to check if he has the permission to access the Form or not). I tried different approaches like Calling string permission = FormLogin.clsUser.permission(); but its not fine .

thanks for your help or any suggestions !!

int id;
    string fname;
    string lname;
    string uUsername;

    public clsUser()
    { }

    public int UserID
    {
        get { return id; }
        set { id = value; }
    }


    public string FirstName
    {
        get { return fname; }
        set { fname = value; }        
    }


    public string LastName
    {
        get { return lname; }
        set { lname = value; }
    }

    public string Username
    {
        get { return uUsername; }
        set { uUsername = value; }
    }


    public override string ToString()
    {
        return base.ToString();
    }
Was it helpful?

Solution

You can access to this object over the whole application by making it static:

public static class clsUser() { }

Now you can access its properties with:

string userPermission = clsUser.Permission;

You can set a property by:

clsUser.Permission = "Administrator";

You dont have to create a new Instance of a static class. You can access to it over the whole application by calling it with its class name (in your case clsUser) and the property name you want to access like above written.

Hope this is useful ;)

OTHER TIPS

you can create a Global class using the singleton pattern. Inside that you can hold your actual User as a Property and access it via

var user = Global.Current.User;
var permission = Global.Current.User.Permission;

I found a topic for the singleton pattern here Thread Safe C# Singleton Pattern

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top