Rajan Mishra

Rajan Mishra

  • 421
  • 3.3k
  • 50k

How to avoid multiple class instances?

Sep 26 2018 2:26 AM
Here I have a sample code in which i am creating the instances of the class and passing more than one time as a parameter. The thing that is bothering me, is why I have to create multiple instances of a single class in all saperate methods is there any possible way to avoid the repetiton of codes.
  1.   public void method1()
  2. {
  3.    class1 cl= new class1();
  4.    c1.filldata();
  5.    method2(c1);
  6. }
  7. public string method2(class1 c)
  8. {
  9.    class1 c2= new class1();
  10.    c2=c;
  11.    string data =c2.data;
  12. }
 Here in the above code snippet i have instantiated two instance of a class which i want to avoid.
 
NOTE: I can not use parametrised class object that is passed to method2. This is because i need  a new object. 

Answers (1)