Then it gives me a null reference exception error. So the object User doesnt exist yet. But I thought the constructor would initiate the whole class to be able to acces it later. How does this work and what do i need to do?
I am not sure why you'd have a static reference to the same class within the class but you have only initialized the parent, not the child. This will fix it it:
//when initializing User do this
User user = new User();
user.Current = new User(); // or user.Current = user; // but I don't know why you do this??? Also strange to have static reference in a non-static class?
HTH's
Regards,
BoogleC
Please mark the right solution as the answer. This helps others find what they're looking for.
I have seen severall projects where they use a current object to be able to access at all time the properties of that object. For example a User logs in and then his data is being saved within this current object. This makes it possible to verify everytime
if the current user is still the same user that uses the computer. Or it makes it possible to get other data that only belongs to that User.
I know you need to initialize it, but at what place? Because the data shouldnt be lost after some method. It should maintain as long the application runs.
SpaceLama
Member
171 Points
136 Posts
Current object
Dec 23, 2012 08:15 PM|LINK
Hello,
I have the following class:
public class User ()
{
public User () {}
public int ID { get; set; }
public string UserName { get; set; }
public static User Current { get; set; }
}
So when I say within my login controller:
User.Current.ID = id;
Then it gives me a null reference exception error. So the object User doesnt exist yet. But I thought the constructor would initiate the whole class to be able to acces it later. How does this work and what do i need to do?
Greetings,
Spacelama
BoogleC
Contributor
2208 Points
445 Posts
Re: Current object
Dec 23, 2012 09:36 PM|LINK
Hi,
I am not sure why you'd have a static reference to the same class within the class but you have only initialized the parent, not the child. This will fix it it:
HTH's
BoogleC
Please mark the right solution as the answer. This helps others find what they're looking for.
SpaceLama
Member
171 Points
136 Posts
Re: Current object
Dec 23, 2012 09:41 PM|LINK
I have seen severall projects where they use a current object to be able to access at all time the properties of that object. For example a User logs in and then his data is being saved within this current object. This makes it possible to verify everytime if the current user is still the same user that uses the computer. Or it makes it possible to get other data that only belongs to that User.
I know you need to initialize it, but at what place? Because the data shouldnt be lost after some method. It should maintain as long the application runs.
ignatandrei
All-Star
135120 Points
21675 Posts
Moderator
MVP
Re: Current object
Dec 23, 2012 10:41 PM|LINK
1. do not put into static something realted to the user - if you have more than 1 user logged, it's a concurrency issue
2. study about session and Session_Start from global.asax