Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7854365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:02:45+00:00 2026-06-02T20:02:45+00:00

I’ve created a very simple helper class that I can use in my ASP.Net

  • 0

I’ve created a very simple helper class that I can use in my ASP.Net pages. The idea is that it is supposed to be a very simple way to log an on-page error or success (not a form validation error) and then display it to the user.

In my public helper class I have a class which has certain properties, as shown below:

public class UserMessage
{
    public UserMessage()
    {
        Messages = new Dictionary<string, string>();
    }

    public string SummaryMessage;
    public Dictionary<string, string> Messages;
    public bool ShowMessages;
    public bool ShowAsError;
}

I then have a variable which is used to store an instance of the UserMessage class, like so:

private static UserMessage _userMessage { get; set; }

I then have two public static methods, one to log a message, the other to display all the messages, like so:

public static void LogSummary(string summaryMessage, bool showIndividualMessages, bool showAsError)
{
    _userMessage = new UserMessage();
    _userMessage.SummaryMessage = summaryMessage;
    _userMessage.ShowMessages = showIndividualMessages;
    _userMessage.ShowAsError = showAsError;
}

public static string DisplayUserMessages()
{
    if (_userMessage == null)
        return string.Empty;

    StringBuilder messageString = new StringBuilder();
    messageString.AppendFormat("\n");
    messageString.AppendLine(string.Format("<div class=\"messageSummary {0}\">", (_userMessage.ShowAsError) ? "invalid" : "valid"));
    messageString.AppendLine(string.Format("<h3>{0}</h3>", _userMessage.SummaryMessage));
    messageString.AppendLine("</div>");

    return messageString.ToString();
}

The problem I have is that the _userMessage variable has to be a static variable, otherwise I get the error message “An object reference is required for the non-static field…….”. The problem with the variable being static is that is stays in memory, so if the user receives an error message and then visits another page – the error message is still displayed!

I’m sure this is because I missed OOP 101, but how should I correct this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:02:46+00:00Added an answer on June 2, 2026 at 8:02 pm

    Do not use static variable to keep messages per user! ASP.NET application is multi-threaded and using static variable is not thread safe. Store them in Session.

    public static void LogSummary(string summaryMessage, ...)
    {
       HttpContext.Current.Session["userMessages"] = new UserMessage(); 
       ...
    }
    
    public static string DisplayUserMessages()
    {
       // get the value from session
       var userMessage = (UserMessage)HttpContext.Current.Session["userMessages"];
       // do the work
       // do the clean up
       HttpContext.Current.Session["userMessages"] = null;
       // the messages will not be displayed on next request
    }
    

    Each request is handled by different thread, so the users will overwrite the _userMessage field and you cannot guarantee that messages for the current user will be displayed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.