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 7670557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:50:59+00:00 2026-05-31T15:50:59+00:00

I have a simple web site built with asp.net. It typically only has 1

  • 0

I have a simple web site built with asp.net. It typically only has 1 or 2 users at one time. My question is, is it ok to instantiate a class at the class level or should I be instantiating for each method. Here is an example. I have a class named Host with a name field and mac field. In my code behind for a specific page Is it ok to do this:

public partial class addhosts : Page
{
     private Host host = new Host();
     private HostDal dal = new HostDal();

     protected void myMethod()
     {
          host.Name = "myname"
          host.Mac = "mymac"
     }

     protected void btnSubmit_Click(object sender, EventArgs e)
     {
          dal.AddHost(host)
     }
}         
  • 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-05-31T15:51:00+00:00Added an answer on May 31, 2026 at 3:51 pm

    First, what you are referring to are more typically referred to as global versus local variables.

    In the simple case that you have listed, it would be better to create the variable on the submit click. The reason is if a user loads the object, but never calls the submit click, then you have instantiated the host object in memory when there was no need.

    However, as many have said, it should not really matter one way or the other here. But, again, this is a simplistic example. Global variables can be dangerous and are often avoided as they can be modified from anywhere in your class. If one method expects a certain value that is then overrode, this can cause difficult to debug issues in more complex examples

    Here is a wikipedia article that reiterates my above point:

    They are usually considered bad practice precisely because of their
    non-locality: a global variable can potentially be modified from
    anywhere (unless they reside in protected memory or are otherwise
    rendered read-only), and any part of the program may depend on it

    To get rid of the globals, you could do this (using object initializers)

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
          var host = new Host
              {
                  Name = "myname",
                  Mac = "mymac"
              };
          dal.AddHost(host)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web site built using asp.net with ugly URLs like /DisplayContent.aspx?id=789564 .
We have a web site built as an 'ASP.NET Website' type project, rather than
Background: I have a website that has been built with ASP.NET 2.0 and is
I have an ASP.NET website that I built that has grown considerably in size
I just built a asp.net 2.0 web site. Now I need add REST web
I have a simple web site ( http://www.kousenit.com/twitterfollowervalue ) that computes a quantity based
I have a simple PowerShell script that uses WMI to create a web site
Here's the setup. I have web site which is essentially a simple CMS. Here
I want to setup a simple site and I don't have any web development
We have a simple registration form for our website users where we only require

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.