none
Error while creating new instance of class:The name employeeName does not exist in the current context RRS feed

  • Question

  • Hi,
    I am using two classes (one of them derived) in an application. When I am trying to create a new object from the derived class I am getting the following error.

    The name employeeName does not exist in the current context
    The name employeeNumber does not exist in the current context.
    ... etc.

    I cannot figure out the cause of the errors. I would appreciate any help for resolution of this issue. Thanks.

    CODE:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace EmployeeProductionWorker
    {
       class Employee
       {
          //Fields
          private string _employeeName;
          private int _employeeNumber;

          //Constructor
          public Employee(string employeeName, int employeeNumber)
          {
             _employeeName = employeeName;
             _employeeNumber = employeeNumber;

          }

          //EmployeeName property
          public string EmployeeName
          {
              get { return _employeeName; }
              set { _employeeName = value; }
          }

          //EmployeeNumber property
          public int EmployeeNumber
          {
            get { return _employeeNumber; }
            set { _employeeNumber = value; }

          }
       }
    }

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace EmployeeProductionWorker
    {
       class ProductionWorker : Employee
       {
          //Field 
          private int _shiftNumber;
          private decimal _hourlyPay;


          //Constructor

          public ProductionWorker(string employeeName, int employeeNumber, int shiftNumber, decimal hourlyPay) : base(employeeName, employeeNumber)
          {
             _shiftNumber = shiftNumber;
             _hourlyPay = hourlyPay;

          }


          //ShiftNumber property
          public int ShiftNumber
          {
             get { return _shiftNumber; }
             set { _shiftNumber = value; }
          }

          //hourlyPay property
          public decimal hourlyPay
          {
             get { return _hourlyPay; }
             set { _hourlyPay = value; }
          }

       }
    }

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

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

    namespace EmployeeProductionWorker
    {
       public partial class Form1 : Form
       {
          public Form1()
          {
             InitializeComponent();
          }

          //The GetProductionData method accepts ProductionWorker object as an argument. It assigns the data entered by the user to the object's properties.
          

          private void GetProductionData(ProductionWorker productionemployee)
          {
             //Temporary variables to hold employeeNumber
             int myemployeeNumber, myshiftNumber;
             decimal myhourlyPay;

             //Get the employee Name
             productionemployee.EmployeeName = employeeNameTextBox.Text;

             //Get the employee Number

             if (int.TryParse(employeeNumberTextBox.Text, out myemployeeNumber))
             { 
                     productionemployee.EmployeeNumber = myemployeeNumber;

                    //Get the shift number

                    if (int.TryParse(shiftNumberTextBox.Text, out myshiftNumber))
                    {
                         productionemployee.ShiftNumber = myshiftNumber;

                              //Get the hourly pay

                                   if (decimal.TryParse(hourlyPayTextBox.Text, out myhourlyPay))
                                   {
                                         productionemployee.hourlyPay = myhourlyPay;
                                   }
                                   else
                                   {
                                         //Display an error message
                                         MessageBox.Show("Invalid hourly pay");
                                   }



                    }

                         else
                    {
                        // Display an error message
                        MessageBox.Show("Invalid shift number");

                    }

             }

             else
             {
              //Display an error message 
               MessageBox.Show("Invalid employee number.");
            }

          }



          private void createObjectButton_Click(object sender, EventArgs e)
          {
             //Create a ProductionWorker object.

             ProductionWorker myProductionWorker = new ProductionWorker(employeeName, employeeNumber, shiftNumber, hourlyPay);      ******** THIS IS THE LINE WHERE THE ERROR OCCURS**********

             //Get the ProductionData

             GetProductionData(myProductionWorker);



          }
       }
    }

    Thursday, November 16, 2017 6:45 PM

Answers

  • Where do you (think) declare employeeName?


    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by gamaz123 Friday, November 17, 2017 1:17 AM
    Thursday, November 16, 2017 7:52 PM

All replies

  • Where do you (think) declare employeeName?


    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by gamaz123 Friday, November 17, 2017 1:17 AM
    Thursday, November 16, 2017 7:52 PM
  • Thanks for your advise. Yes it worked great after I declared and initialized the arguments I am passing to the new instance of the called object.  

    CODE:

      private void createObjectButton_Click(object sender, EventArgs e)
          {
             //Declare and initialize the arguments to be passed to the new instance of productionworker object.

             string employeeName = "";
             int employeeNumber = 0;
             int shiftNumber = 0; ;
             decimal  hourlyPay = 0.0m;
          
             //Create a ProductionWorker object.

             ProductionWorker myProductionWorker = new ProductionWorker(employeeName, employeeNumber, shiftNumber, hourlyPay);

             //Get the ProductionData

             GetProductionData(myProductionWorker);

             MessageBox.Show(myProductionWorker.EmployeeName);
             MessageBox.Show(myProductionWorker.EmployeeNumber.ToString());
             MessageBox.Show(myProductionWorker.ShiftNumber.ToString());
             MessageBox.Show(myProductionWorker.hourlyPay.ToString());
          }

    Friday, November 17, 2017 1:17 AM