D
D
Denis2014-10-28 18:01:10
.NET
Denis, 2014-10-28 18:01:10

Is it possible to organize shared variables between some methods?

private void SomeCoolrMethod()
{
for (var i = 0; i < 90000; i++)
{
   var pamar1 = SetParam1();
   var pamar2 = SetParam2();
   var pamar3 = SetParam3();
   var pamar4 = SetParam4();
   var pamar5 = SetParam5();
   
   DoMethod1(param1, param3, param4);

   DoMethod2(param1,param2, param4);
   
   DoMethod3(param3,param4,param5);
}
}

Is it possible to move the "paramN" variables outside the "SomeCoolMethod" method, but so that no other methods except "SomeCoolMethod" and "DoMethodN" have access to them?
A sort of "private && only for methods(....)" modifier.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
smet4ik, 2014-10-28
@evolit

Why, for example, not to pack it all in a separate class? And do all the work there?

class SomeCool
    {
        private T pamar1;
        private T pamar2;
        private T pamar3;
        ...

        private void SomeCoolrMethod()
        {
            for (var i = 0; i < 90000; i++)
            {
                var pamar1 = SetParam1();
                var pamar2 = SetParam2();
                var pamar3 = SetParam3();
                var pamar4 = SetParam4();
                var pamar5 = SetParam5();

                DoMethod1(param1, param3, param4);

                DoMethod2(param1, param2, param4);

                DoMethod3(param3, param4, param5);
            }
        }
    }

A
AlexP11223, 2014-10-28
@AlexP11223

Closure or what?
en.wikipedia.org/wiki/Closure_%28computer_programm...

A
aush, 2014-10-28
@aush

Func Delegate :

void MyMethod()
{
    Func<int> pamar1 = () => 1 * 2;
    Func<int> pamar2 = () => 3 * 4;
    Func<int> pamar3 = () => 5 * 6;

    for (var i = 0; i < 10; ++i)
    {
        DoMethod(pamar1, pamar2, pamar3);
    }
}

void DoMethod(Func<int> pamar1, Func<int> pamar2, Func<int> pamar3)
{
    Console.WriteLine(pamar1() * pamar2() * pamar3());
}

However, I'm not sure I understood your question correctly. Try to be more coherent about what you are trying to achieve.

R
Raphael, 2014-10-28
@dj_raphael

What are you afraid of? that the developer of another method in this class is using your variables? Well, scold him or something, explain the essence of the class in the comments, write tests.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question