Question

pass a variable as parameter to constructor as i want to change the variable value with changeable value

i'm new to c# so that i tries to implement one thing in many way i did what i want to do with using ( function ) and worked

namespace testConstructors
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        static void Persons(string DepartmentName)
        {
          System.Windows.Forms.MessageBox.Show("Your Department is " + DepartmentName + " Fixed salary = 1500");
        }

        private void btnShowTotalSalary_Click(object sender, EventArgs e)
        {
            int fixedSalary = 1500;
            if (rbtHR.Checked)
            {
                lblTotalSalary.Text= Convert.ToString(fixedSalary + 200);
                Persons("HR");
            }
            if (rbtDevelopers.Checked)
            {
                lblTotalSalary.Text= Convert.ToString(fixedSalary + 8000);
                Persons("developer");
            }
            if (rbtTesters.Checked)
            {
                lblTotalSalary.Text = Convert.ToString(fixedSalary + 2);
                Persons("tester");
            }
        }

        private void rbtHR_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}

but when i want to use to classes and take one class and add aconstructor to it and pass the value through it as this code shown ,it worked but not changed in everey time change the radio button as first code did .

so i want to know is this thing can be done by constructor or not

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

        private void btnShowTotalSalary_Click(object sender, EventArgs e)
        {
            string DeptName;
            Persons p2 = new Persons("HR");
            if (rbtHR.Checked)
            {
                lblTotalSalary.Text= Convert.ToString(p2.fixedSalary + 200);
                DeptName = "HR";
            }
            if (rbtDevelopers.Checked)
            {
                lblTotalSalary.Text= Convert.ToString(p2.fixedSalary + 8000);
                DeptName = "Developers";
            }
            if (rbtTesters.Checked)
            {
                lblTotalSalary.Text = Convert.ToString(p2.fixedSalary + 2);
                DeptName = "Testers";
            }
        }

        private void rbtHR_CheckedChanged(object sender, EventArgs e)
        {

        }
    }


    class Persons
    {
        public int fixedSalary;
        public Persons(string DepartmentName)
        {
            fixedSalary = 1500;
            System.Windows.Forms.MessageBox.Show("Your Department is " + DepartmentName + " Fixed salary = 1500");
        }
    }
}
Was it helpful?

Solution

Instead of passing hardcoded department name to the constructor of Person class, pass the initialized DeptName variable to Person class.

    private void btnShowTotalSalary_Click(object sender, String e)
    {
        string DeptName = "Department";

        if (rbtHR.Checked)
        {
            lblTotalSalary.Text= Convert.ToString(p2.fixedSalary + 200);
            DeptName = "HR";
        }
        if (rbtDevelopers.Checked)
        {
            lblTotalSalary.Text= Convert.ToString(p2.fixedSalary);
            DeptName = "Developers";
        }
        if (rbtTesters.Checked)
        {
            lblTotalSalary.Text = Convert.ToString(p2.fixedSalary + 2);
            DeptName = "Testers";
        }

        Person p2 = new Person(DeptName);
    }
Licensed under: CC-BY-SA with attribution
scroll top