jeudi 15 mars 2018

pass an object or the field of an object to a method of the other object?

When a function of an object(class1) needs to access one field (or maybe more) of the other object(class2), you pass the field (or fields) or the object as an argument?

Also about functions of an object, you pass the function as an action or a delegate or the whole object as a argument?

public class Class1{
    public void Func1(string str){  
       //
    }
    public void Func11(Action<> action){  
       //
    }
}

//or
public class Class1{
    public void Func1(Class2 cl2){  
       //
    }
}

public class Class2{
   private string _field2;
   public string Field2{
      get{
         return _field2;
      }
      private set{
         _field2=value;
      }
   }
   public void Func2(/*arguments*/){
      //
   }
}

// call it
Class1 cl1=new Class1();
Class2 cl2=new Class2();

cl1.Func1(cl2);
//or
cl1.Func1(cl2.Field2);
cl1.Func11(cl2.Func2)

Aucun commentaire:

Enregistrer un commentaire