0

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();
    }
user3213767
  • 179
  • 2
  • 2
  • 9

2 Answers2

2

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

Relax
  • 283
  • 2
  • 15
  • Thanks for your help. to make sure about your solution, i think i should also define the class in the next forms as usual (private clsUsers form2Class = new clsUsers()), but through the static keyword, the values will be already there, right ??? – user3213767 Apr 04 '14 at 10:10
  • @user3213767 you dont have to create a new instance of a static class. I will edit my answer to explain it a bit more ;) – Relax Apr 04 '14 at 10:25
  • Thanks alot, it works perfeclty now !! great !!. can i ask you another relative question please ? static fucntions can also have paramepters, for example ClassDB Help functions, i think i could make them static also and i could sind them parameters like (SQLString, etc. ), right ? – user3213767 Apr 04 '14 at 10:57
  • @user3213767 properties and other methodes dont have to be static because the class is already static. You can define them as public. You can set all parameters you want to ;) – Relax Apr 04 '14 at 11:07
1

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

Community
  • 1
  • 1
Roman Mahrer
  • 186
  • 1
  • 4