This is my class Designation.
class Designation
{
private string strDesigNo;
private string strDesigName;
private string strDesigDesc;
public string DesigNo
{
set { strDesigNo = value; }
get { return strDesigNo; }
}
public string DesigName
{
set { strDesigName = value; }
get { return strDesigName; }
}
public string DesigDesc
{
set { strDesigDesc = value; }
get { return strDesigDesc; }
}
}
In my user interface form on click of Save button I assign the values to my Department class and then send it to save method in another class as follows.
private void btnSave_Click(object sender, EventArgs e)
{
if(!string.IsNullOrEmpty(txtDesignationNumber.Text.Trim().ToString()))
{
if (!string.IsNullOrEmpty(txtDesignationName.Text.Trim().ToString()))
{
if(!string.IsNullOrEmpty(txtDesignationtDescription.Text.Trim().ToString()))
{
objDesignation.DesigNo = txtDesignationNumber.Text.Trim().ToString();
objDesignation.DesigName = txtDesignationName.Text.Trim().ToString();
objDesignation.DesigDesc=txtDesignationtDescription.Text.Trim().ToString();
objDesignationBLL.insertDesignation(objDesignation);
}
}
}
}
// What I need is immediately after sending the object values to insertDesignation method,clear all the values.
//That means I need it as follows.
objDesignation.DesigNo ='';
objDesignation.DesigName = '';
objDesignation.DesigDesc = '';
Is there a good practice to empty these object variable values to null without disposing the object or without setting the object to null?
If you want to reset your variable, you could just assign a new instance:
objDesignation = new Designation();
Well, I suggest using local variable for the local task
Designation objDesignation = new ...
// Let's avoid arrow head antipattern:
// if any of Text is significant (i.e. has non-whitespace items)
// then insert a designation
if (new [] {
txtDesignationNumber.Text,
txtDesignationName.Text,
txtDesignationtDescription.Text}
.Any(c => !String.IsNullOrWhiteSpace(c)) {
// create and insert a Designation;
// there's no need to expose dsgn to the outer scope
var dsgn = new Designation() {
DesigNo = txtDesignationNumber.Text.Trim(),
DesigName = txtDesignationName.Text.Trim(),
DesigDesc = txtDesignationtDescription.Text.Trim()
};
objDesignationBLL.insertDesignation(dsgn);
}
// objDesignation hasn´t been changed
objDesignation...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With