Clear object variables

This is my class assignment.

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, when I click the Save button, I assign values ​​to my department class and then submit it to save the 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);
            }
        }
    }
}

// I need immediately after sending the values ​​of objects to the insertDesignation method, clear all values.

// This means that I need it as follows.

objDesignation.DesigNo ='';

objDesignation.DesigName = '';

objDesignation.DesigDesc = '';

Is there a good practice for these values ​​of an object variable to be deleted to zero without deleting the object or setting the object to null?

+4
source share
3 answers

If you want to reset your variable, you can simply assign a new instance:

objDesignation = new Designation();
+11
source

:

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

Clear(), reset :

objDesignation.Clear()

Designation:

objDesignation = new Designation()

.


, . , .

+1

Well, I suggest using a local variable for a local task

  Designation objDesignation = new ...

  // Let 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 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...
+1
source

Source: https://habr.com/ru/post/1627945/


All Articles