1

I have the following scenario: a program I made has three type of user accounts: superuser, administrator and consultant.

The login forms works well and through a method I can get the type of the user and open the respective main form of said user.

The problem is this: there are some forms that the users share. But, I don't know how to have the application know which of the three main forms it must return depending of the user.

Question is: there is a way for keep the value (user type) from the login form and use it on the other forms?

Here is how I get the value of type in the database:

  public string sacartipo()
    {string tipo = "";
       username = usuario.Text;
        obj.Usuario = usuario.Text;
        password = contra.Text;
        obj.Contrasena = contra.Text;
         tipo = obj.Logeo(username, password);

        return tipo; //This is the variable that stores the type of user. 

    }
Linkark07
  • 25
  • 4
  • Please use more tags. C# doesn't say much. Is it a web app, desktop app? I guess you are talking about asp.net – ZolaKt Dec 29 '12 at 16:27
  • Maybe this can help you: http://stackoverflow.com/questions/1205195/how-to-pass-values-between-forms-in-c-sharp-windows-application? – Randy Levy Dec 29 '12 at 17:49

4 Answers4

1

Make every form's constructor accept the user type, so that whenever you show a new form you tell it which user will use it.

BlackBear
  • 22,411
  • 10
  • 48
  • 86
1

You can store the user type(after the user logs in) in a session variable like Session["role"]=value and use it anywhere you want.

Srinivas
  • 1,063
  • 7
  • 15
  • I think this is a windows application since he mentions `forms` so he can't use Session. – Mahdi Tahsildari Dec 29 '12 at 16:28
  • Hi Mahdi...He did not mention it as windows form.... he just said login form...there are web forms too...so you cannot come to such a conclusion. – Srinivas Dec 29 '12 at 16:36
  • Yeah, it is a Windows Application. Sorry for not mentioning it. – Linkark07 Dec 29 '12 at 16:46
  • then declare the variable as a public variable in form1 and just use it as form1.variablename in the other forms... – Srinivas Dec 29 '12 at 17:05
  • Doesn't work. Since the variable is inside a method when I try to call it in another form it appears as null. – Linkark07 Dec 29 '12 at 17:47
  • @Linkark07 :I mentioned the easy way above....If you want an alternative, you can use a global get/set from a class.I found a link which has an example http://forums.devshed.com/c-programming-42/c-passing-variable-between-windows-forms-511521.html – Srinivas Dec 30 '12 at 14:03
  • Thanks Srinivas. Had a hard time figuring out how to do this. – Linkark07 Dec 30 '12 at 15:38
0

You can store users in an enum

public enum Users { Superuser, Administrator, Consultant }

and have a static object of Users type in your application that is seen everywhere (global)

public Users User;

on Login assign the appropriate value to the User static object, for eg.

MyApplication.User = Users.Consultant;

and in each form check user's permission on Load, eg.

var adminPageAllowedUsers = new[] {Users.SuperUser, Users.Administrator};
if(admingPageAllowedUsers.Contains(MyApplication.User))
{
    //do the rest
}
else
{
    //redirect user to another form
}

this is the logic I wanted to give you, and the codes are not tested, so excuse me for possible errors.

Mahdi Tahsildari
  • 13,065
  • 14
  • 55
  • 94
0

Assume sacartipo() was a method of Form1 and you want to call it an get the return value, you add the following method in Form2:

string GetTipo() {
    var currentContext=SynchronizationContext.Current;
    var tipoLocal=default(string);

    SendOrPostCallback d=
        dummy => {
            using(var x=new Form1())
                tipoLocal=x.sacartipo();
        };

    if(null!=currentContext)
        currentContext.Send(d, null);
    else
        d(null);

    return tipoLocal;
}

In the code of Form2, you call GetTipo() and what you expect to do have been done.

Ken Kin
  • 4,503
  • 3
  • 38
  • 76