Home > Back-end >  why is my method not returning a value when called?
why is my method not returning a value when called?

Time:11-09

I'm trying to create a program that takes user input and passes it to three methods.

  • one method calculates total pay
  • one method determines shift selected
  • I want the last method to be the display method, but I've used a method that receives parameters to pass the information, a void method, and static which caused more problems than it helped.

Why is the method not getting the information?

This class has it's own code file called Employee.

Class method to display results:

//Method to display results
public void Display(double total)
{
    //main form instance to access controls
    MainForm mainForm = new MainForm();


    mainForm.nameSumBox  .Text = Name;
    mainForm.empNumSumBox.Text = EmployeeNumber.ToString();
    mainForm.paySumBox   .Text = total.ToString();
    mainForm.shiftSumBox .Text = SelectedShift;
}

Call-site:

        try
        {
            //variables
            double totalPay;

            //get user entry
            employee.Name = nameBox.Text;
            employee.EmployeeNumber = int.Parse(empNumBox.Text);
            employee.HRPay = double.Parse(hrPayBox.Text);
            employee.SelectedShift = employee.ShiftChoice(); // method to determine shift selection

            //calculate pay total
            totalPay = employee.CalcPay();

            //display
            employee.Display(totalPay);

CodePudding user response:

when using Poul's solution it was a success!

for the main file when calling the method: employee.Display(this, totalPay);

CodePudding user response:

Display Method should be

public void Display(MainForm mainForm, double total)
{
    mainForm.nameSumBox.Text = Name;
    mainForm.empNumSumBox.Text = EmployeeNumber.ToString();
    mainForm.paySumBox.Text = total.ToString();
    mainForm.shiftSumBox.Text = SelectedShift;
}

pass the mainform as parameter to the funcion.

  • Related