Forums - C# Corner

Forum guidelines
Junaid Syed

Junaid Syed

  • 1.4k
  • 44
  • 1.3k

Can we use single class object twice in single method

Aug 16 2017 2:34 AM


  1. if (User.Count != 0 || Email.Count != 0)  
  2. {  
  3.     Session["EmailErrorMessage"] = "Username or Email already exists";  
  4. }  
  5.   
  6. else if (User != null && UserMail != null && Designation != 5)  
  7. {  
  8.     var employee = new Employees()  
  9.     {  
  10.         EmployeeID = UserName,  
  11.         Password = encerptd,  
  12.         Email = UserMail,  
  13.         FirstName = UserFirstName,  
  14.         LastName = UserLastName,  
  15.         Role = (int)Helpers.GlobalsVariables.Role.Entry,  
  16.         IsActive = true,  
  17.         CreatedDate = DateTime.Now,  
  18.         CreatedBy = 1,  
  19.         ModifiedBy = 1,  
  20.         Manager = Manager,  
  21.         ModifiedDate = DateTime.Now,  
  22.         Designation = UserDesignation  
  23.     };  
  24.     int employeeID = _employees.InsertAndReturnPk(employee);  
  25.     Session["successMessage"] = "User has been Created Successfully";  
  26. }  
  27. else  
  28. {  
  29.     var employee = new Employees()  
  30.     {  
  31.         EmployeeID = UserName,  
  32.         Password = encerptd,  
  33.         Email = UserMail,  
  34.         FirstName = UserFirstName,  
  35.         LastName = UserLastName,  
  36.         IsActive = true,  
  37.         Role = (int)Helpers.GlobalsVariables.Role.Admin,  
  38.         CreatedDate = DateTime.Now,  
  39.         CreatedBy = 1,  
  40.         ModifiedBy = 1,  
  41.         ModifiedDate = DateTime.Now,  
  42.         Designation = UserDesignation,  
  43.         Manager = null  
  44.     };  
  45.     int employeeID = _employees.InsertAndReturnPk(employee);  
  46.     Session["successMessage"] = "User has been Created Successfully";  

instead of creating class object multiple time in single method.Can i create class object only single time and using multiple time in single method.
Here is my code in conditon.I need to create class object for single time and use in if conditon and else condition with different parameter it that possible to do to resude my code of lines .Please help me 

Answers (3)