Reputation: 305
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?
Upvotes: 9
Views: 33206
Reputation: 186698
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...
Upvotes: 3
Reputation: 14417
This is how I would re-structure that class:
class Designation
{
public string Number { get;set; }
public string Name { get;set; }
public string Description { get;set; }
public void Clear()
{
this.Number = string.Empty;
this.Name = string.Empty;
this.Description = string.Empty;
}
}
I've added the method Clear()
which should reset the properties when called:
objDesignation.Clear()
I personally would just new up a instance of the Designation
again:
objDesignation = new Designation()
once you are done with the data.
As an aside, I would advise you make your variable names as explicit as possible. Just so it is easier in the long run to remember what was going on.
Upvotes: 1
Reputation: 156978
If you want to reset your variable, you could just assign a new instance:
objDesignation = new Designation();
Upvotes: 20