Hi Matthew,
There are a few ways to accomplish your goal but I've included the simplest example I could think of here:
internal sealed class User
{
/// <summary>Gets the currently logged in <see cref="User" /or <c>null</c>.</summary>
public static User LoginUser { get { return loginUser; } } // note the "static" keyword
// instance properties (not "shared")
public string UserName { get { return userName; } }
public string Password { get { return password; } }
// private, "shared" reference to the currently logged in user
private static User loginUser; // note the "static" keyword
// private instance (not "shared") fields
private string userName, password;
// using a single, private constructor prevents instances of this class from being created externally (by other classes other
than User itself)
private User()
{
}
/// <summary>Logs in a <see cref="User" /with the specified <paramref name="userName" /and <paramref name="password"
/>.</summary>
/// <param name="userName">Name of the user to be logged in.</param>
/// <param name="password">Password of the user to be logged in.</param>
public static User Login(string userName, string password)
{
if (loginUser != null)
throw new InvalidOperationException("A user is already logged into the application: " + loginUser.UserName);
User user = new User();
user.userName = userName;
user.password = password;
// TODO: login user with supplied credentials
// store user in static field for "shared" access
loginUser = user;
return user;
}
}
The User class can be used as such:
// Login the user using the static Login method:
User user = User.Login("user name", "the password");
// Later, in code where you don't have that user variable (it is out of scope)
// the logged in User can be retrieve through the static LoginUser property:
User user = User.LoginUser;
string loginUserName = user.UserName;
(Please note that I didn't try to build this code. If you have any problems building it, or understanding it for that matter, then
just let me know and I'll try to help)
I used the static keyword on the loginUser field and the LoginUser property so that the logged in User can be referenced in code
without the need of a User instance. I believe VB used "Modules" for this type of functionality, however Modules have a global
visibility, IIRC. In C# you can only access the static members above by explicitly referencing the User class: User.Login("name",
"password") and User.LoginUser, as in my code sample.
(Note: I seem to remember that shared was a common VB term, so I laced the comments above with the term "shared" to make things
clearer to you, however I recommend that you get used to using the term "static" instead if you aren't already. ;)
Another common way to retrieve the login user is to create an IIdentity implementation (yes, with two I's) and add it to a new
GenericPrincipal instance when the user first logs in. Then, assign the principal to the current Thread via the static
Thread.CurrentPrincipal property. It can be retrieved at anytime by any code that executes on that Thread. (see the
System.Security.Principal namespace and the System.Threading namespace).
--
Dave Sexton
"Matthew" <ma******@yahoo.comwrote in message news:11**********************@b28g2000cwb.googlegr oups.com...
Sorry Dave,
Ok the question is, how do I go about setting a global isntance of the
a user class in which I can call anytime to retrieve the user that is
logged in into the application. I know this should be something I
should know, but I am used to VB, and learning C# by implementing it
into my own application, basically trying to learn it on my own.