3

I'm wanting to declare a variable as below for use in several pages on my site. I assume rather than declaring it once per page I can do it globally? I've tried it in a class (app_code folder) and in the global.asax page but my code can't seem to find it. I may be going abo9ut it all wrong though? Code as below...

Dim myUser As MembershipUser = Membership.GetUser()

So basically as it stands my pages can't find the 'myUser' variable. Any help much appreciated! Thanks

5 Answers 5

4

Use the Session or Application objects, if you need such a "global".

Use Session if needed per user, Application if for the whole application.

Sign up to request clarification or add additional context in comments.

2 Comments

If it's the current user I guess it's a Session variable then. If I put that line in my global.asax file it doesn't work however. Where should I be putting it?
@adrian g - What line? Have you changed it to use sessions? I suggest you read the linked document to see how Session and Application should be used.
2

Create a class/struct and add a static

namespace MyApp
{
    public class Variables
    {
        public static User MembershipUser
        {
            get { return Membership.GetUser(); }
        }
    }
}

Then you can access the value/property using:

MyApp.Variables.MembershipUser

If this GetUser() returns a user object with a property "Username" you can:

Hello <%=MyApp.Variables.MembershipUser.Username%>

Here's a VB.NET version:

 Namespace MyApp
    Public Class Variables
        Public Shared ReadOnly Property MembershipUser() As User
            Get
                Return Membership.GetUser()
            End Get
        End Property
    End Class
End Namespace

4 Comments

Ok I've put the following in a class file: Public Class Variables Public Shared myuser As MembershipUser = Membership.GetUser() End Class and called it using Variables.myUser
You do not want static (shared) user-specific variables! A static variable has the same value for all website users! A static property returning a user-specific value is fine.
Sorry I think I'm misunderstanding your comment. You've said that I do not want static user-specific variables, but I do want static user-specific values?
variables and properties are not the same. You should use a static Property as my example
0

This could go in a page base class or in an extension method to System.Web.UI.Page

public  static MembershipUser GetMembershipUser(this System.Web.UI.Page page)
{
    return Membership.GetUser();
}

Comments

0

@stian.net I have the following - works exactly as I want it to. Would you say this was the correct way to do it?

Public Class Variables
Public Shared ReadOnly Property myUser As MembershipUser
    Get
        Return Membership.GetUser()
    End Get
End Property
End Class

Comments

0

Try this:

Application("MembershipUser") = "J. Doe"

Retrieve it with:

MyUser$ = Application("MembershipUser")

Note: Global application variables are stored as strings, so if you'd need to store objects, you'll have to serialize them into strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.