Public Property in ASP.net using VB.net

Hello,

I am using session.item("username") in many places in my ASP.net application. if I declare a Public property, can I just use the propertyname instead of using session.item("username") in many places??

if yes.

How do I create a public property which can store username and how do I retreive the property and use it in all my other pages.
sureshrainaAsked:
Who is Participating?
 
Carl TawnSystems and Integration DeveloperCommented:
To use it, you wrap it in a class, like:

Public Class UserDetails
    Public Shared Property Username As String
        Get
            Return Session("username")
        End Get
        Set (ByVal value As String)
            Session("username") = value
        End Set
    End Sub
End Class

Then in your code you access it with something like:

   Response.Write(UserDetails.Username)
0
 
Carl TawnSystems and Integration DeveloperCommented:
You could create a class with a shared method and have that return the username...if thats what you mean.


Public Shared Property Username As String
    Get
        Return Session("username")
    End Get
    Set (ByVal value As String)
        Session("username") = value
    End Set
End Sub

Essentially this creates a wrapper around the session variable.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.