Creating an object that has the entire application scope.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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:

Wonderful suggestion. Thanks.

Tom Shelton said:
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.
 
Back
Top