Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

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");
        }
    }
}
share|improve this question

closed as unclear what you're asking by Bart van Ingen Schenau, Ixrec, whatsisname, gnat, MichaelT Apr 12 at 1:27

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

up vote 0 down vote accepted

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, EventArgs e)
    {
        string DeptName = "Unknown Department";

        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";
        }

        Persons p2 = new Persons(DeptName);
    }
share|improve this answer
    
Cannot use local variable 'p2' before it is declared, thanks for your help . i think that no need to use class or constructor in this case .but i was asking if there are available to pass a variable as parameter to constructor –  mohamedhashim Apr 11 at 17:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.