locked
Error Message: Does not exist in the current context RRS feed

  • Question

  • I keep getting an error message that 'newName', 'newAddress', 'newZip' does not exist in the current context. I can't figure out why. Help!

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace CustomerApplication
    {
        public partial class addCustomerForm : Form
        {   //Constructor method
            public addCustomerForm()
            {
                InitializeComponent();
            }// End of Constructor method
            // saveAndExit_Click method
            private void saveExitBtn_Click(object sender, EventArgs e)
            {
                //save the data first
                saveNewCustomerInfo();
                //next, retrieve hidden form's memory address
                Form myParentForm = CustomerAppStart.getParentForm();
                //now that we have the address use it to display the parentForm
                myParentForm.Show();
                //Finally close this form
                this.Close();
            }// end saveAndExitBtn_Click method

            //cancelBtn_Click method
            private void cancelBtn_Click(object sender, EventArgs e)
            {
                //retrieve hidden form's memory address
                Form myParentForm = CustomerAppStart.getParentForm();
                //now that we have the address use it to display the parentForm
                myParentForm.Show();
                //Finally close this form
                this.Close();
            }
                //end cancelBtn_Click
            //save new customer info
            private void saveNewCustomerInfo()
            {
                //get the values from the textBoxes
                String newName = custNameField.Text;
                String newAddress = custAddressField.Text;
                int newZip = 0;
                try
                {
                    newZip = Convert.ToInt32(custZipField.Text);
                }
                catch (Exception e)
                {
                }
                // create a new customer object
                Customer newCustObj = new Customer();
                // set the cust obj. attributes
                newCustObj.setCustomerName(newName);
                newCustObj.setCustomerAddress(newAddress);
                newCustObj.setCustomerZip(newZip);
                // save the new customer object
                Customer.saveACustomer(newCustObj);
            }
        private void mod_Click(object sender, EventArgs e)
            {
            Customer t = new Customer();
            t.setCustomerName(newName);
            t.setCustomerAddress(newAddress);
            t.setCustomerZip(newZip);



            }//end save customer
        }// end add customer for class definition
    }

        
    Thursday, December 13, 2012 1:50 AM

Answers

  • Are these the statements where you see the errors?

     private void mod_Click(object sender, EventArgs e)
            {
            Customer t = new Customer();
            t.setCustomerName(newName);
            t.setCustomerAddress(newAddress);
            t.setCustomerZip(newZip);

    The variables newName, newAddress and newZip are out of scope in the mod_Click method because they are declared as local variables in your saveNewCustomerInfo method.

    If you want to use newName, newAddress and newZip in your mod_Click method, you need to declare them at class level so they can be used in all your class methods.

    If I have misunderstood your issue, please let me know.


    Thursday, December 13, 2012 4:29 AM
  • Hi Riced,

      Welcome to MSDN Forum Support.

      GC will collection would do this work,detailed information you can refer to Garbage Collection. If you manually collect the garbage,you can set the instance of strings or ints with null and excute GC.Collect method to collect the garbage.

      Sincerely,

      Jason Wang

                                      


    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us


    Thursday, December 20, 2012 6:12 AM
    Moderator

All replies

  • Are these the statements where you see the errors?

     private void mod_Click(object sender, EventArgs e)
            {
            Customer t = new Customer();
            t.setCustomerName(newName);
            t.setCustomerAddress(newAddress);
            t.setCustomerZip(newZip);

    The variables newName, newAddress and newZip are out of scope in the mod_Click method because they are declared as local variables in your saveNewCustomerInfo method.

    If you want to use newName, newAddress and newZip in your mod_Click method, you need to declare them at class level so they can be used in all your class methods.

    If I have misunderstood your issue, please let me know.


    Thursday, December 13, 2012 4:29 AM
  • Are these the statements where you see the errors?

     private void mod_Click(object sender, EventArgs e)
            {
            Customer t = new Customer();
            t.setCustomerName(newName);
            t.setCustomerAddress(newAddress);
            t.setCustomerZip(newZip);

    The variables newName, newAddress and newZip are out of scope in the mod_Click method because they are declared as local variables in your saveNewCustomerInfo method.

    If you want to use newName, newAddress and newZip in your mod_Click method, you need to declare them at class level so they can be used in all your class methods.

    If I have misunderstood your issue, please let me know.



    yes and global variables better be disposed well

    Please remember to mark the replies as answers if they help and unmark them if they provide no help , or you may vote-up a helpful post

    Thursday, December 13, 2012 11:24 AM
  • Hmm. How does on dispose of strings or ints since neither implements IDisposable?

    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Thursday, December 13, 2012 11:54 AM
  • Hi Riced,

      Welcome to MSDN Forum Support.

      GC will collection would do this work,detailed information you can refer to Garbage Collection. If you manually collect the garbage,you can set the instance of strings or ints with null and excute GC.Collect method to collect the garbage.

      Sincerely,

      Jason Wang

                                      


    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us


    Thursday, December 20, 2012 6:12 AM
    Moderator
  • Jason

    Thank you for a belated welcome - I've been a member since Feb 2009.

    Also you don't need to tell me about GC - the question was meant ironically (read the message I replied to).

    The answer is 'you don't since they do not implement IDisposable'. Allowing the GC to retrieve the memory from the heap is not the same as disposing of strings or ints. Unless you simply mean allow them to become inaccessible.

    BTW your reply is not an answer to the OP - please unmark it as such.


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Friday, December 21, 2012 12:34 PM