Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear object variables

Tags:

c#

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?

like image 963
code_Finder Avatar asked Feb 09 '16 11:02

code_Finder


2 Answers

If you want to reset your variable, you could just assign a new instance:

objDesignation = new Designation();
like image 193
Patrick Hofman Avatar answered Sep 30 '22 07:09

Patrick Hofman


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...
like image 22
Dmitry Bychenko Avatar answered Sep 30 '22 07:09

Dmitry Bychenko