By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,369 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,369 IT Pros & Developers. It's quick & easy.

Creating an object that has the entire application scope.

P: n/a
Hello:

I am thinking of an object called "user." This object will be instatntiated
during a login process. But I want to use this to control the behavior of the
application since it will also encapsulate the logged-in user's security
profile.

What is the best strategy for creating one such object?

I initially thought of having a module wherein it is declared public. Even
if I do, I may need to pass in as a parameter by reference to the Login form
and by value to other forms. Would I have to have parameters in the forms'
New subroutine? Or, is there a more elegant way to make this exposed
throughout the application?

Venkat

Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 2004-10-26, vvenk <vv***@discussions.microsoft.com> wrote:
Hello:

I am thinking of an object called "user." This object will be instatntiated
during a login process. But I want to use this to control the behavior of the
application since it will also encapsulate the logged-in user's security
profile.

What is the best strategy for creating one such object?

I initially thought of having a module wherein it is declared public. Even
if I do, I may need to pass in as a parameter by reference to the Login form
and by value to other forms. Would I have to have parameters in the forms'
New subroutine? Or, is there a more elegant way to make this exposed
throughout the application?

Venkat


You could always create the object as a singleton...

Public Class User
Private Shared instance As User

' Allow no uncontroled instances
Private Sub New ()
End Sub

' Create a shared constructor - this
' will be called sometime before first access
Shared Sub New ()
instance = new User ()
End Sub

' do all your properties/methods

Public Shared Function GetInstance () As User
Return User.instance
End Sub
End Class

This is a very simple example - but essentially what this allows is that
anywhere in your code you can say:

Dim currentUser As User = User.GetInstance ()
' Do stuff

And all parts of your program will refere to the single shared instance
of the User class.

--
Tom Shelton [MVP]
Nov 21 '05 #2

P: n/a
Tom:

Wonderful suggestion. Thanks.

"Tom Shelton" wrote:
On 2004-10-26, vvenk <vv***@discussions.microsoft.com> wrote:
Hello:

I am thinking of an object called "user." This object will be instatntiated
during a login process. But I want to use this to control the behavior of the
application since it will also encapsulate the logged-in user's security
profile.

What is the best strategy for creating one such object?

I initially thought of having a module wherein it is declared public. Even
if I do, I may need to pass in as a parameter by reference to the Login form
and by value to other forms. Would I have to have parameters in the forms'
New subroutine? Or, is there a more elegant way to make this exposed
throughout the application?

Venkat


You could always create the object as a singleton...

Public Class User
Private Shared instance As User

' Allow no uncontroled instances
Private Sub New ()
End Sub

' Create a shared constructor - this
' will be called sometime before first access
Shared Sub New ()
instance = new User ()
End Sub

' do all your properties/methods

Public Shared Function GetInstance () As User
Return User.instance
End Sub
End Class

This is a very simple example - but essentially what this allows is that
anywhere in your code you can say:

Dim currentUser As User = User.GetInstance ()
' Do stuff

And all parts of your program will refere to the single shared instance
of the User class.

--
Tom Shelton [MVP]

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.