Skip to content

Common property and methods between classes

john ranella

Asked Sep 11, 2015, 1:24 AM EDT2 answers
Share
LinkedInX
Hi,
I have some classes that share between their methods and properties: for example class A call method of class B and both class must have common field/property
Common methods/property are several!
I thought of this approach
C#
//------------------------------------------------------
// CLASS 'A'
//------------------------------------------------------
public class A
{


  public A Instance{get{return this;}}
  public IB b;


  public float FieldA;


  public void DoSomethingA()
  {
    FieldA = b.DoSomethingB ();
  }


}


//------------------------------------------------------
// INTERFACE + CLASS 'B'
//------------------------------------------------------
public interface IB
{
  B Instance{get;}
  A a {get;set;}
  float DoSomethingB();
}


public class B:IB
{


  public B Instance{get{return this;}}
  public A a {get;set;}


  public virtual float DoSomethingB()
  {
    return a.FieldA * 0.5F;
  }
}


//------------------------------------------------------
// CLASS B2
//------------------------------------------------------
public class B2:B
{

  public override float DoSomethingB ()
  {
    return a.FieldA  * 2.0F;
  }


}




//------------------------------------------------------
// CLASS TEST
//------------------------------------------------------


  public class test
{


  public A  a = new A();
  public IB b;


  public test (IB b)
  {


    this.b = b;


    b.a = a.Instance;
    a.b = b.Instance;
    // other




  }
}






    public static void Main (string[] args)
  {


    test t = new test (new B());


    t.a.FieldA = 5;
    t.a.DoSomethingA();


    Console.WriteLine (t.a.FieldA ); // return 2.5




    test t1 = new test (new B2());  // return 10
    t1.a.FieldA = 5;
    t1.a.DoSomethingA();

    Console.WriteLine (t1.a.FieldA );



  }
 
Essentially in the class test pass the class instance A to the class B and vice versa (and thus also for future classes).
When i create a new instance of test i decide if use class B or B2 (trough interface).
This approach is correct or i'm in a wrong way?

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Minhaj Hussain
    0
    Through Interface IB it is much better, now your code is loosely coupled but if u wanna be too strict then dont make the function virtual in B so that only this function will be called
    0
  • 0
    Hi Minhaj Hussain,
    and thanks for response.
    you said that "through Interface IB it is much better, now your code is loosely coupled....."
    but if i not use Interface:

    //------------------------------------------------------
    // CLASS 'A'
    //------------------------------------------------------
    public class A
    {

    public A Instance{get{return this;}}
    public B b;

    public float FieldA;

    public void DoSomethingA()
    {
    FieldA = b.DoSomethingB ();
    }

    }

    //------------------------------------------------------
    //  CLASS 'B'
    //------------------------------------------------------

    public class B
    {

    public B Instance{get{return this;}}
    public A a {get;set;}

    public virtual float DoSomethingB()
    {
    return a.FieldA * 0.5F;
    }
    }

    //------------------------------------------------------
    // CLASS B2
    //------------------------------------------------------
    public class B2:B
    {

    public override float DoSomethingB ()
    {
    return a.FieldA * 2.0F;
    }

    }


    //------------------------------------------------------
    // CLASS TEST
    //------------------------------------------------------

    public class test
    {

    public A a = new A();
    public B b;

    public test (B b)
    {

    this.b = b;

    b.a = a.Instance;
    a.b = b.Instance;
    // other


    }
    }



    public static void Main (string[] args)
    {

    test t = new test (new B());

    t.a.FieldA = 5;
    t.a.DoSomethingA();

    Console.WriteLine (t.a.FieldA ); // return 2.5


    test t1 = new test (new B2()); // return 10
    t1.a.FieldA = 5;
    t1.a.DoSomethingA();

    Console.WriteLine (t1.a.FieldA );


    }


    the results is the same, but now code is highly coupled?
    Can you explain more about this concept? (I am a beginner in c #)

    About virtual method i need to specialize class B. there is another best way to do this?

    thanks in advance
    0