user990635
user990635

Reputation: 4249

Instance of the class inside itself

If I have a class with public methods (not static) that I can't change.
And I need to add some of my own methods that will use the existing public non static methods,

how can I do it in the right way?

Right now, in my own methods I'm creating an instance of my own class to use the other methods, but I don't think this is a good way.

Example:

class aaa
{

  public string a(int i)
  {
    ...
  }
  public string b (int i)
  {
    ...
  }

  public static void x()
  {
    aaa myclass = new aaa();
    string str = myclass.a(5);
  }

}

Upvotes: 2

Views: 3068

Answers (8)

Vivek
Vivek

Reputation: 2123

Exact Use Case for Extension Methods imho.

You need to attach behaviour (new methods) to a closed class right. So just go:

class aaa
{
  public string a(int i)
  {
    ...
  }
  public string b (int i)
  {
    ...
  }
}

static class ExtensionContainer
{
  public static void x(this aaa myclass)
  {
    string str = myclass.a(5);
  }
}

The extension method(s) will be callable just like instance methods, so the caller provides the object needed for the functions you need to use.

If you actually have a requirement to have the new method be static - that's just a huge code smell. Static methods being closer to pure functions - and using an internal stateful object for implementation?

If it's NOT a sign of bad design, i don't know the siutation, just make a singleton of your class (aaa) and use that object for implementing the method.

Upvotes: 0

Abbas
Abbas

Reputation: 14432

I think your design is not completely correct but if you cannot change your design this might do the trick. Although I also think that this way of working is not the correct way.

public class MyClass
{
    public string MethodA(int i)
    {
        return StaticMethodA(i);
    }

    private static string StaticMethodA(int i)
    {
        return String.Format("i is {0}", i);
    }

    public static void MethodX()
    {
        string str = StaticMethodA(5);
    }
}

Now you'll be able to do:

MyClass mc = new MyClass();
string str = mc.MethodA(3);

//or

MyClass.MethodX();

Upvotes: 3

Servy
Servy

Reputation: 203802

It sounds like you really wish your instance method was static; it should have been static from the start since it doesn't rely on using any internal state of the object (since your new method isn't being given, giving, or re-using any instance of that type). You just can't make the breaking change to make it static because there are too many dependencies using it in a non-static context.

If you can't change the method to be static even though it logically is, your solution is indeed the best you can do in the situation. It's essentially creating a static "window" into the non-static method.

Upvotes: 1

Ondrej Svejdar
Ondrej Svejdar

Reputation: 22054

Two options:

Don't write static method:

class Other {
  private aaa _MyClass = new aaa();
  public void x() {
    string str = _MyClass.a(5);
  }
}

Declare aaa as static instance:

static aaa s_MyClass = new aaa();
public static void x() {
  string str = s_MyClass.a(5);
}

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236188

To call non-static method you must have instance of class, which instance method you want to call. Also thus you can't change class aaa, then you are able only to extend it with extension methods or inherit from that class. Here is extension method sample:

public static class MyExtensions
{
    public static void x(this aaa myClass)
    {
       // use public methods of class you are extending
       string str = myclass.a(5);
    }
}

You can execute x method like a normal method of myClass instance:

aaa myclass = new aaa();
myClass.x(); // you extension 

BTW in C# we use PascalNames for classes and methods.

Upvotes: 0

stepandohnal
stepandohnal

Reputation: 457

If you cannot change the class use extension methods. link

Upvotes: 0

Nick Humrich
Nick Humrich

Reputation: 15755

This is where inheritance becomes useful. The syntax for this in c# is:

public class myClass : aaa
{

Then all you have to do is put your methods in this class. The other methods will work just as well.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

What you are describing looks very much like extension methods of C#. Define them in a separate static class, and use them as if they were instance methods.

Upvotes: 2

Related Questions