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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:00:05+00:00 2026-05-31T00:00:05+00:00

I have a program in which the user inputs information into two separate text

  • 0

I have a program in which the user inputs information into two separate text boxes and then clicks a button, at which time the program does some work and returns data to a third text box.

Then the user enters data into another textbox and clicks a second button. The program takes the text box return data from the first button click method along with the new input data, does some work and returns data to a mutli-line text box.

The problem I am having is that once the program leaves the first button click method, the return data disappears, so no data from the first button click method gets passed to the second button click method.

I am pretty sure it does this because the object is instantiated inside of the first button click method instead of being instantiated at the class level and therefore once the program leaves the method the object is gone. I attempted to instantiate the object at the class level but I cannot because I receive a “field initializer cannot reference the non-static field, method, or property” error.

I have looked at the MSDN specifications for this problem and understand what causes it, but the answer they give to fix it is to instantiate the object inside of the method. So I am right back to where I started from.

I am at a loss on what to do here. Here is my code with the object instantiated inside of the button click method:

private void btnOrderButton_Click(object sender, EventArgs e)
    {              
        numberOfPizzas = int.Parse(this.txtNumberOfPizzaOrdered.Text);
        numberOfCokes = int.Parse(this.txtNumberOfCokesOrdered.Text);
        PizzaOrder orderObject = new PizzaOrder(numberOfPizzas, numberOfCokes);

        this.txtAmountDue.Text = orderObject.TotalAmountDue.ToString("C");                                  
    }

    private void btnPaymentButton_Click(object sender, EventArgs e)
    {
        amountDue = double.Parse(this.txtAmountDue.Text);
        amountPaid = double.Parse(this.txtAmountPaid.Text);

        Payment orderPaymentObject = new Payment(amountDue, amountPaid);
    }
  • 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-31T00:00:07+00:00Added an answer on May 31, 2026 at 12:00 am

    The data disappears because you’re creating a temporary object at the method’s scope, meaning it doesn’t exist outside the method.

    To fix that declare the data member at the class level and use it like so:

    Payment m_orderPaymentObject = null;
    private void btnPaymentButton_Click(object sender, EventArgs e)
    {
        amountDue = double.Parse(this.txtAmountDue.Text);
        amountPaid = double.Parse(this.txtAmountPaid.Text);
    
        m_orderPaymentObject = new Payment(amountDue, amountPaid);
    }
    

    Repeat the process for any other data object you need.

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

Sidebar

Related Questions

I have a program in which the user adds multiple objects to a scene.
I have a program in which a user selects a row in a Datagrid
I have a project to create a program, which prevents the user from escaping
I have a program which needs installing on windows 64 boxes. Most of the
I am adding a feature to my program in which the user will have
I have a UIPicker where the user inputs a specified time (i.e. 13:00, 13:01,
I am making a time sheet program where a user inputs his in- and
i am working on a program which draws polygons according to user inputs. I
I have a list of string. When user inputs chars in, the program would
I have this program which takes a array of words and asks user to

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.