kittu
kittu

Reputation: 7018

How to set the values of private members using public property?

This is my class:

 class EmpDetails
    {
        private string _EmpName;
        private int _EmpID;
        private string _EmpDepartment;
        private string _EmpPosition;

        public string EmpName
        {
            get
            {
                return _EmpName;
            }
            set
            {
                _EmpName = value;
            }
        }
        public int EmpID
        {
            get
            {
                return _EmpID;
            }
            set
            {
                _EmpID = value;
            }

        }
        public string EmpDepartment
        {
            get
            {
                return _EmpDepartment;
            }
            set
            {
                _EmpDepartment = value;
            }
        }
        public string EmpPosition
        {
            get
            {
                return _EmpPosition;
            }
            set
            {
                _EmpPosition = value;
            }
        }
    }
}

Following is my form:

public partial class Form1 : Form

    {
        EmpDetails d = new EmpDetails();
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            d.EmpName = txtName.Text;
            d.EmpID = Convert.ToInt32(txtID.Text);
            d.EmpDepartment = txtDepartment.Text;
            d.EmpPosition = txtPosition.Text;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtName.Clear();
            txtID.Clear();
            txtDepartment.Clear();
            txtPosition.Clear();
        }

        private void btnGet_Click(object sender, EventArgs e)
        {
            txtName.Text = d.EmpName;
            txtID.Text = Convert.ToString(d.EmpID);
            txtDepartment.Text = d.EmpDepartment;
            txtPosition.Text = d.EmpPosition;
        }
    }
}

I am setting the values using text boxes in form so that the values go in to properties I have created in class.

I'm getting error like: EncapsulationAssignmentCSharp.EmpDetails does not contain a definition for GetEmpName and no extension method GetEmpName accepting a first argument of type EncapsulationAssignmentCSharp.EmpDetails could be found (are you missing a using directive or an assembly reference?

I am guessing that I have to create a constructor with parameters and set the values using keyword this, but I'm not sure how to pass the values to the constructor. Please help me I am not very good with programming.

Upvotes: 0

Views: 156

Answers (1)

user4277706
user4277706

Reputation:

Debug and run the code and check whether your code is calling GetEmpName

Upvotes: 1

Related Questions