vb.net setting the value of a property

This might be a best practices question.
I have created a class called CurrentUserInfo  I want to be able to use the class to grab information about the currently logged in user anywhere in the project (asp.net)

in my mind I thought I could define the properties and have the values set by a function within the same class...
I wanted to do this because it is information that does not change and as soon as the user opens the page the information is available

so would look something like this

Public Shared sUserName As String = GetUserName()

Open in new window

As you might have guessed vb dosent like that
my questions are
1) Can this be done at all - where by retrieving the value of the property from another form it causes the property to fill itself (clear as mud)

2) should I be doing this a completely different way...
David ModugnoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David ModugnoAuthor Commented:
Here is the function to get the currently logged in username

 Function GetUserName() As String
        If TypeOf My.User.CurrentPrincipal Is
      Security.Principal.WindowsPrincipal Then
            ' The application is using Windows authentication.
            ' The name format is DOMAIN\USERNAME.
            Dim parts() As String = Split(My.User.Name, "\")
            Dim username As String = parts(1)
            Return username
            ' The application is using custom authentication.
            Return My.User.Name
        End If
    End Function

Open in new window

Jacques Bourgeois (James Burger)PresidentCommented:
What you have created is not a property, it is a static (shared) field.

Your problem is that you are using My. My was designed to make things easier for amateur programmers. It is a fast way to get most things done, but it is very limited in scope.

True, the framework documentation can be intimidating, but taking the time to search in there will save you a lot of coding.

You can get the same result as what you were trying to do simply by asking for Environment.UserName. And not need to record that in a class or variable, the Environment class already does that for you. Simply call it whenever you need it.
David ModugnoAuthor Commented:
Thank you for the response....  The username was only an example..
another would be that I need to pull the users employee number from a sql DB..
I will be using the username to pull that number.

in the class there will be other properties such as emp num, phone number, address, and more importantly some permission information

throughout the project I will be looking at the value in the permissions property as the user attempts to perform various functions (all of this was defined years ago)

back to the original question is there a way to all of the properties fill themselves with values... the values will all be based off of the currently logged in user

Thanks again for the response
Jacques Bourgeois (James Burger)PresidentCommented:
Then, define your class with properties, not with Shared fields.

A Shared field is a value that will be shared by all the instances of the class. That means that if you change it for 1 employee, you are changing it for all the other employees. Probably not what you want to do with your class. As an example, a Shared field could be useful for a Report class, where you set the Printer once so that it can be used by all the other printers.

Define properties instead, with the following syntax if you are not in an old version of Visual Studio.

Public property UserName As String = GetUserName()

Note that the convention, in an object environment, specially when there are over 10,000 types of variables to work with, is not to use a prefix.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David ModugnoAuthor Commented:
Thank you helped a lot... I appreciate you taking the time to explain some of this..
Makes a lot more since now
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.