brad119
brad119

Reputation: 71

How to pass an instance of a class to another form c#

So at the top of top of the main window class I declare an instance of my staff class calling it staff1

public partial class MainWindow : Window
{

    Staff staff1 = new Staff();
    public double grossPay;
    public double taxRate;
    public MainWindow()
    {           
        InitializeComponent();
    }

    private void btnSet_Click(object sender, RoutedEventArgs e)
    {
        staff1.staffID = Convert.ToInt32(txtStaffID.Text);
        staff1.firstName = txtFirstName.Text;
        staff1.surname = txtSurname.Text;
        staff1.dob = txtDOB.Text;
        staff1.department = txtDepartment.Text;
        staff1.hourlyPayRate = Convert.ToDouble(txtPayRate.Text);
        staff1.hoursWorked = Convert.ToDouble(txtHoursWorked.Text);
   }

    private void btnClear_Click(object sender, RoutedEventArgs e)
    {
        txtStaffID.Text = "";
        txtFirstName.Text = "";
        txtSurname.Text = "";
        txtDOB.Text = "";
        txtDepartment.Text = "";
        txtPayRate.Text = "";
        txtHoursWorked.Text = "";
    }

    private void txtDOB_TextChanged(object sender, TextChangedEventArgs e) { }

    private void btnGet_Click(object sender, RoutedEventArgs e)
    {
        txtStaffID.Text = Convert.ToString(staff1.staffID);
        txtFirstName.Text = staff1.firstName;
        txtSurname.Text = staff1.surname;
        txtDOB.Text = staff1.dob;
        txtDepartment.Text = staff1.department;
        txtPayRate.Text = Convert.ToString(staff1.hourlyPayRate);
        txtHoursWorked.Text = Convert.ToString(staff1.hoursWorked);
    }

    private void btnCalcPay_Click(object sender, RoutedEventArgs e)
    {
        grossPay = staff1.calcPay();
        taxRate = staff1.calcTaxRate();
        Payslip P = new Payslip();
        P.Show();     
    }
}

}

I then have another form called Payslip, all I want to do is be able to access the staff1 instance of my class in the new form but I can't

public partial class Payslip : Window
{
    public Payslip()
    {
        InitializeComponent();
       // Want to be able to access staff1 here ????
    }
}

}

Upvotes: 2

Views: 4212

Answers (2)

Arif YILMAZ
Arif YILMAZ

Reputation: 5866

you can pass Staff as a parameter to the constructor.

public partial class Payslip : Window
{
    Staff staff_;
    public Payslip()
    {
        InitializeComponent();
        //Want to be able to access staff1 here ????
    }

    public Payslip(Staff staff)
    {
        InitializeComponent();
        //Want to be able to access staff1 here ????
        staff_ = staff;
    }

}

Upvotes: 0

Selman Genç
Selman Genç

Reputation: 101681

Just pass it via constructor

// make a constructor in the second form that takes Staff as parameter
Staff _staff;
public Payslip(Staff s)
{
   InitializeComponent();
   _staff = s;
}

private void btnCalcPay_Click(object sender, RoutedEventArgs e)
{
    grossPay = staff1.calcPay();
    taxRate = staff1.calcTaxRate();
    // pass the staff1 instance to constructor when creating the Form 
    Payslip P = new Payslip(staff1); 
    P.Show();
}

Upvotes: 2

Related Questions