Getting rid of function's argument

Posted by Max on Stack Overflow See other posts from Stack Overflow or by Max
Published on 2012-11-23T22:26:10Z Indexed on 2012/11/23 23:04 UTC
Read the original article Hit count: 119

Filed under:
|
|

It is better to explain my intention with code. So right now I have the following code:

class A, IInterfaceUsedByB
{
}

class B
{
    void func(A someObject)
    {
        func2(someObject, 1);
        func3(someObject, "string");
        func4(someObject, new MyObject());
    }
    func2(A someObject, int val);
    func3(A someObject, string val);
    func4(A someObject, C val);
}

Where func2, func3, func4 do need references to someObject. I want to change this to

void func()
{
    with(someObject,
            () =>
                {
                    func2(1);
                    func3("string");
                    func4(new MyObject());
                }
        );
}

Or even better to

void func(someObject)
{
    func2(1);
    func3("string");
    func4(new MyObject());
}

So that I don't have to drag this someObject around, but I should still be able to use it inside func2,3,4. I can use any of the three languages (C#, F# or IronPython) for this.

UPDATE In the ideal solution class B would be independent of A. func* functions only depend on a small interface of A consisting of 2 methods.

© Stack Overflow or respective owner

Related posts about c#

Related posts about F#