5

There are two files A.cs and B.cs. There is a method fn() which is used in both the classes.

Method fn() is used in both class files. This increases code complexity if I need this method in many class files (say 100 files).

I know that we can call this method by creating an object for the class in which this method is defined. How can I share this function between two or more classes without creating an object every time for accessing this method?

Anandaraj
  • 161
  • 1
  • 3
  • 13
  • Why does it increase complexity if 100 classes call your method? For example, if i use the method `String.Contains` in 100 classes the method itself remains simple and readable. – Tim Schmelter Aug 22 '13 at 09:15
  • 2
    make it a static method? – Yellowfog Aug 22 '13 at 09:16
  • 1. Create a static class with this method as static method 2. Make base class, inherit all the class from this base class. Imlement this method in the base class – Anand Aug 22 '13 at 09:16
  • Create a class that contains all your common methods. Then A.cs to Z.cs can always use the common method in Common.cs – BossRoss Aug 22 '13 at 09:17
  • @TimSchmelter If 100 classes call this method, then I will have to create 100 objects for the same class in different class files. – Anandaraj Aug 22 '13 at 09:18
  • @Anandaraj: Ok, then make it static if it doesn't need to access instances anyway. – Tim Schmelter Aug 22 '13 at 09:22

3 Answers3

9

Put the method in a static class:

public static class Utils
{
     public static string fn()
     {
         //code...
     }
}

You can then call this in A.cs and B.cs without creating a new instance of a class each time:

A foo = new A();
foo.Property = Utils.fn();

Alternatively, you could create a BaseClass that all classes inherit from:

public class BaseClass
{
    public BaseClass() { }
    public virtual string fn()
    {
        return "hello world";
    }
}

public class A : BaseClass
{
    public A() { }
}

You would then call fn() like so:

A foo = new A();
string x = foo.fn();
DGibbs
  • 14,316
  • 7
  • 44
  • 83
2

I hope the function isn't really called fn(), but actually named to what it does, like CalculateTotal(). Then you can extract this method into a class, say: TotalCalculator.

Now upon application startup, preferably using dependency injection, you create one instance of the class that gets shared between objects that require it. Like so:

class TotalCalculator
{
    public int Calculate()
    {
        return 42;
    }
}

class NeedsCalculator1
{
    TotalCalculator _calculator; 

    public NeedsCalculator1(TotalCalculator calculator)
    {
        _calculator = calculator; 
    }

    public void Foo()
    {
        _calculator.Calculate();
    }
}

class NeedsCalculatorToo
{
    TotalCalculator _calculator; 

    public NeedsCalculatorToo(TotalCalculator calculator)
    {
        _calculator = calculator; 
    }

    public void Bar()
    {
        _calculator.Calculate();
    }
}

Then you instantiate the calculator once, and pass it into the other classes' constructor:

TotalCalculator calculator = new TotalCalculator();

NeedsCalculator1 dependency1 = new NeedsCalculator1(calculator);
NeedsCalculatorToo dependency2 = new NeedsCalculatorToo(calculator);

You can now further abstract the calculator dependency by creating a base class containing the constructor and a protected TotalCalculator instance field, for example.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • 1
    Is dependency injection really necessary here? – dtsg Aug 22 '13 at 09:27
  • Is `static` really necessary here? We don't know what `fn()` does or whether it or calls to it ever need to be mocked. – CodeCaster Aug 22 '13 at 09:30
  • Well it just seems like overkill IMO for what could very well be a simple problem. Always best to start small. Just my 2 cents.. – dtsg Aug 22 '13 at 09:36
  • Take a look at [When to Use Static Classes in C#](http://stackoverflow.com/questions/241339/when-to-use-static-classes-in-c-sharp). Constructor injection adds a bit of boilerplate code, but allows for easy extension and testing of the class. OP says he'll be needing this class / method in hundreds of other classes (which itself sounds like a problem to be honest), so I wouldn't go as far as calling this a simple problem. – CodeCaster Aug 22 '13 at 09:56
1

Assuming that this method is self contained, you could create a static class and put this method as a static method in it, which is in turn, called by the other classes.

If is is not self contained, you could try and declare it in some super class and let the other 100 classes extend that class.

npinti
  • 51,780
  • 5
  • 72
  • 96