public object throughout the project

N

NR

I am new in .NET application but have developed many applications in Visual
FoxPro and Visual Basic.
In my first .NET application, I have a USER class which keeps record of user
privileges. Is it possible to instantiate this object once in a project(To
avoid back and forth communication with data server), which can be used in
all other classes and forms.

Any kind of help would be appreciated.

Regards,
NR
 
C

Chris Taylor

Hi,

You should take a look at the singleton pattern at least for an
understanding of the concept. Once you understand the pattern you can look
at how .NET makes implementing this pattern very easy.

Basically you would create an instance of your class as a static member of
some instance manager which can then expose a static function or property
for you to retrieve the instance from anywhere. As a trivial example the
following shows how the UserInfo class could provide both functions. Excuse
any typos, this was just off the top of my head without the IDE (How
dependent have we become...), also I avoided any C# 3.0 specific syntax just
since I did not know what you are using.

public class UserInfo
{
// Here we construct the static instance of UserInfo
private static UserInfo _instance = new UserInfo();

// Property used to access the single instance of the UserInfo class
public static Instance
{
get {return _instance;}
}

// Private instance data for the UserInfo class
private string _userName;
private bool _isAdmin;

public string UserName
{
get {return _userName;}
}

public bool IsAdmin
{
get {return _isAdmin;}
}

// Make the constructor private to ensure that no one constructs an
instance outside of this class
private UserInfo()
{
// Initialize instance members here
}
}

Now you can access this from anywhere using the following syntax

string userName = UserInfo.Instance.UserName;

There are a number of problems with the above approach, especially if you
are doing anything in the constructor that could cause an exception. For
this I would rather explicitly initialize the instance etc. but this should
give you a reasonable starting point.

Hope this helps
 
C

Chris Taylor

Hi,

You should take a look at the singleton pattern at least for an
understanding of the concept. Once you understand the pattern you can look
at how .NET makes implementing this pattern very easy.

Basically you would create an instance of your class as a static member of
some instance manager which can then expose a static function or property
for you to retrieve the instance from anywhere. As a trivial example the
following shows how the UserInfo class could provide both functions. Excuse
any typos, this was just off the top of my head without the IDE (How
dependent have we become...), also I avoided any C# 3.0 specific syntax just
since I did not know what you are using.

public class UserInfo
{
// Here we construct the static instance of UserInfo
private static UserInfo _instance = new UserInfo();

// Property used to access the single instance of the UserInfo class
public static Instance
{
get {return _instance;}
}

// Private instance data for the UserInfo class
private string _userName;
private bool _isAdmin;

public string UserName
{
get {return _userName;}
}

public bool IsAdmin
{
get {return _isAdmin;}
}

// Make the constructor private to ensure that no one constructs an
instance outside of this class
private UserInfo()
{
// Initialize instance members here
}
}

Now you can access this from anywhere using the following syntax

string userName = UserInfo.Instance.UserName;

There are a number of problems with the above approach, especially if you
are doing anything in the constructor that could cause an exception. For
this I would rather explicitly initialize the instance etc. but this should
give you a reasonable starting point.

Hope this helps
 
M

Mr. Arnold

NR said:
I am new in .NET application but have developed many applications in Visual
FoxPro and Visual Basic.
In my first .NET application, I have a USER class which keeps record of
user
privileges. Is it possible to instantiate this object once in a project(To
avoid back and forth communication with data server), which can be used in
all other classes and forms.

Any kind of help would be appreciated.


Yeah, you can do that as long as you pass the public USER object with any
public properties or methods within USER object that can be acted upon
publicly in the USER object, passing USER object in a form's constructor or
in a method's signature as a parameter.


public class USER
{
#region public properties

public long UserID { get; set; }
public string Username { get; set; }

#endregion

#region public methods

public User GetUser(long id)
{
data access to get the data from data-store

UserID = da.UserID;
UserName = da.Username;

return this;
}

#endregion
}


This is about from wherever you instantiate a new USER object.

USER obj = new USER();

USER userobj = obj.GetUser(22);


This is method signature that you have to pass userobj in it.

public void Somemethod(USER userobj)
{
string userid = userobj.UserID.ToString();
}


the call

Somemethod(userobj);


call to form passing userobj

FormA theform = new FormA(userobj);


FormA constructor


public FormA(USER userobj)
{
objuser = userobj;
}


Objuser would be a public declaration in FormA.

public USER objuser;






__________ Information from ESET NOD32 Antivirus, version of virus signature database 4080 (20090515) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
M

Mr. Arnold

NR said:
I am new in .NET application but have developed many applications in Visual
FoxPro and Visual Basic.
In my first .NET application, I have a USER class which keeps record of
user
privileges. Is it possible to instantiate this object once in a project(To
avoid back and forth communication with data server), which can be used in
all other classes and forms.

Any kind of help would be appreciated.


Yeah, you can do that as long as you pass the public USER object with any
public properties or methods within USER object that can be acted upon
publicly in the USER object, passing USER object in a form's constructor or
in a method's signature as a parameter.


public class USER
{
#region public properties

public long UserID { get; set; }
public string Username { get; set; }

#endregion

#region public methods

public User GetUser(long id)
{
data access to get the data from data-store

UserID = da.UserID;
UserName = da.Username;

return this;
}

#endregion
}


This is about from wherever you instantiate a new USER object.

USER obj = new USER();

USER userobj = obj.GetUser(22);


This is method signature that you have to pass userobj in it.

public void Somemethod(USER userobj)
{
string userid = userobj.UserID.ToString();
}


the call

Somemethod(userobj);


call to form passing userobj

FormA theform = new FormA(userobj);


FormA constructor


public FormA(USER userobj)
{
objuser = userobj;
}


Objuser would be a public declaration in FormA.

public USER objuser;






__________ Information from ESET NOD32 Antivirus, version of virus signature database 4080 (20090515) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
P

Paul

I do not quite understand your question.

I suggest you look at Authentication, Authorisation for Web/Windows Forms.

I am presuming you wish to cache the User object after login?

If so the Singleton idea above was a good one, as long as the information
does not change with parmeters (Sub Types of User/Authorisation). If it is
changeable then you may wish to consider a Singleton/Factory pattern.
 
P

Paul

I do not quite understand your question.

I suggest you look at Authentication, Authorisation for Web/Windows Forms.

I am presuming you wish to cache the User object after login?

If so the Singleton idea above was a good one, as long as the information
does not change with parmeters (Sub Types of User/Authorisation). If it is
changeable then you may wish to consider a Singleton/Factory pattern.
 

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