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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:00:39+00:00 2026-05-29T11:00:39+00:00

Below is my code behind-file in my project, and as you can see i’m

  • 0

Below is my code behind-file in my project, and as you can see i’m instantiating an object of a class called “SecretNumber” in the Page_Load. I recently had a problem accessing the object reference in the button function, because I forgot to put the reference out of the scope (private SecretNumber guessNr).

However, by some strange reason this code generates the following error: “Field ‘_Default.guessNr’ is never assigned to, and will always have its default value null.”

If I delete “private SecretNumber guessNr” (which is generating the error) the problem not being able to reach the object reference from the button function is there again, so obvious the reference is assigned with the object.

How come I get this error and how to solve it?

public partial class _Default : System.Web.UI.Page
{
    private SecretNumber guessNr;

    protected void Page_Load(object sender, EventArgs e) {
        SecretNumber guessNr = new SecretNumber();
    }

    protected void btnCheckNr_Click(object sender, EventArgs e) {
        if (!Page.IsValid){
            return;
        }

        // The rest of the code goes here
    }
}
  • 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-29T11:00:40+00:00Added an answer on May 29, 2026 at 11:00 am

    You need to make a subtle change inside your Page_Load method

    private SecretNumber guessNr; // declared field here
    
    protected void Page_Load(object sender, EventArgs e) { 
        guessNr = new SecretNumber();
    } 
    

    The way you have it, you are declaring a local variable of the same name. It is hiding the class-level field while inside the method.

    int x; // this is a class level field
    int y; // same
    
    public void Foo() 
    {
        int x; // this is a local of the same name
        this.x = 7; // this is how you would access the class field from this method
        y = 4; // no need for "this." for disambiguation
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the code below in code behind file's Page_Load event: LinkButton linkButton =
I have an application that's instantiates an object from a class called SecretNumber when
I have below code behind in c# if (Session[cmpDictionaryTitle]!= null) { downloadLinks.Text += @<li><a
I created a DatePicker user control (ASP code below, no code behind) which is
The below code in Java throws Null pointer exception. public class New{ int i;
Inside an event in my code behind file I want to run a script
I am generating one PDF from the Code Behind File using StringWriter and HtmlTextWriter.
I have the below code in a .ascx file as part of an asp.net
I have the working code pasted below which has a function called FormatStatus() as
I have XAML File as below: <Window x:Class=ComboBoxCheck.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:check=clr-namespace:ComboBoxCheck Title=Window1 Height=300 Width=320>

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.