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

  • SEARCH
  • Home
  • 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 100057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:30:23+00:00 2026-05-11T00:30:23+00:00

I have inherited a project that uses the following pattern for passing parameters from

  • 0

I have inherited a project that uses the following pattern for passing parameters from the code behind to the aspx page. I know this is wrong, but I am on the fence on the best way to refactor it.

Code Behind:

using System; using System.Web;  namespace BadPassing {     public partial class _Default : System.Web.UI.Page     {         private Random rnd = new Random(); //Is this bad?          protected int numberOne; //this is bad         protected int numberTwo; //this is bad          protected void Page_Load(object sender, EventArgs e)         {             numberOne = rnd.Next(100);             numberTwo = rnd.Next(100);         }     } } 

ASPX Page:

<%@ Page Language='C#' AutoEventWireup='true' CodeBehind='Default.aspx.cs' Inherits='BadPassing._Default' %>  <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>  <html xmlns='http://www.w3.org/1999/xhtml' > <head runat='server'>     <title>Bad Page</title> </head> <body>     <form id='form1' runat='server'>     <div>         <a href='http://www.google.com/search?q=<%=this.numberOne %>%2B<%=this.numberTwo %>'>             Add <%=this.numberOne %> and <%=this.numberTwo %> using google.         </a>     </div>     </form> </body> </html> 

My understanding is that the numberOne and numberTwo are not thread-safe, and could cause incorrect behavior if two people loaded the page at the same time. Furthermore, if the page relied on numberOne and numberTwo to store values between postbacks, multiple simultaneous users would cause unexpected results.

Is my understanding of why this the above technique is so bad correct, and if so, how would you best refactor this code?

On a side note, is it incorrect to store stateless page-level services (like Random) as member variables of the page class?

  • 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. 2026-05-11T00:30:24+00:00Added an answer on May 11, 2026 at 12:30 am

    This code is ok. The member variables are instantiated every time the page loads, as a new instance of a class is created on every page load. If the variables were static, only then would you have problems with 2 people loading the page at the same moment.

    Think about the controls you drop on a page. They are member variables, but they don’t have problem with the page being loaded at the same time by different requests.

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

Sidebar

Related Questions

I have inherited a project that uses LLBLGen Pro for the DB layer. The
I have inherited a Websphere Portal project that uses Hibernate 3.0 to connect to
I have a sample project that uses the metrics from notifications for resizing the
I have inherited a code-base that uses ASP.NET MVC 3. I'm learning it as
New to SubSonic, inherited it with a project that uses SubSonic 2.2.1 I have
I have inherited a pure C project that uses GNU Pth ( http://www.gnu.org/software/pth/ )
I have inherited a project that has class libraries written in VB.NET, some of
I have inherited a web project that is perl based and I'm attempting to
I have inherited a web site project that makes use of a number of
I have inherited a project, that provides an order form to a customer. The

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.