Avatar of David Modugno
David Modugno

asked on 

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...
Visual Basic.NETASP.NET

Avatar of undefined
Last Comment
David Modugno
Avatar of David Modugno
David Modugno

ASKER

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
        Else
            ' The application is using custom authentication.
            Return My.User.Name
        End If
    End Function

Open in new window

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.
Avatar of David Modugno
David Modugno

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of David Modugno
David Modugno

ASKER

Thank you helped a lot... I appreciate you taking the time to explain some of this..
Makes a lot more since now
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo