Problem accessing Properties of class, not sure if class


G

Guest

Hi, I've got a User class which holds details of the user and is populated by
a query. I create this User object when the user successfully logs in and
then I set this object to a session variable (its for a ASP.Net site) so that
I can access any of the user's details from this session object.
The problem is when I'm trying to access a Property of the object, the
intellisence does not display the Property name. So when I type in:

Session["user"]. I only get the standard object methods.

This is how my class looks:

public class User
{
private string username;
private string firstname;
private string lastname;
private string password;
private string email;
private string roleID;
protected string userType;

public User(string _username, string _password)
{
Username = _username;
Password = _password;
GetUserDetails(Username, Password);
}

#region "Properties"
public string Username
{
get { return username; }
set { username = value.Trim();}
}

public string Password
{
get { return password; }
set { password = value.Trim(); }
}

public string Firstname
{
get { return firstname; }
set { firstname = value.Trim();}
}

public string Lastname
{
get { return lastname; }
set { lastname = value.Trim(); }
}

public string Email
{
get { return email; }
set { email = value; }
}

public string RoleID
{
get { return roleID; }
set { roleID = value; }
}

public string UserType
{
get { return userType; }
set
{
if (value == "1")
{
userType = "Expert";
}
if (value == "2")
{
userType = "Basic";
}
}
}


#endregion

protected void GetUserDetails(string _username, string _password)
{
DataSet ds = UserDAO.GetUserDetails(_username, _password);

foreach (DataRow dr in ds.Tables["User"].Rows)
{
Firstname = dr["firstname"].ToString();
Lastname = dr["lastname"].ToString();
RoleID = dr["RoleID"].ToString();
UserType = dr["RoleID"].ToString().TrimEnd();

}

}


}


What gives??
 
Ad

Advertisements

M

Marc Gravell

session[whatever] doesn't know (at compile time) what your object is;
you just need to cast to the User:

User user = (User) session["user"];

or whatever; this line could be exposed (via a property) in a
base-class for the pages, so that actually pages just call .CurrentUser
(or whatever) and it is made available easily.

Marc
 
Ad

Advertisements

I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


You can also use ((User)Session["user"]).Property

BTW, it's wise to check for null first, if the session expired you may not
have an instance in the session and you will get an exception.




--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


MicroMoth said:
Thanks Marc,

Marc Gravell said:
session[whatever] doesn't know (at compile time) what your object is;
you just need to cast to the User:

User user = (User) session["user"];

or whatever; this line could be exposed (via a property) in a
base-class for the pages, so that actually pages just call .CurrentUser
(or whatever) and it is made available easily.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top