开发者

Passing parammeters

开发者 https://www.devze.com 2023-03-28 11:27 出处:网络
Hi guys I have one question, If I have a sequence of methods for example: Main() { Method1(); } Method1() { Method2();

Hi guys I have one question,

If I have a sequence of methods for example:

Main()
{
  Method1();
}

Method1()
{
  Method2();
}

Method2()
{
  Method3();
}

Method3()
{
  ObtainsUserPermission(httpContext.Current.User.Name);
}

How is the best way to do it, using the parammeter "httpContext.Current.User.Name" 开发者_如何学Cin the last Method3, or passing by parammeter in each method? Like this:

Main()
{
  Method1(httpContext.Current.User.Name);
}

Method1(string name)
{
  Method2(name);
}

Method2(string name)
{
  Method3(name);
}

Method3(string name)
{
  ObtainsUserPermission(name);
}

thank you for all.


This smells like magic parameters.

A good rule of thumb is - if you wish your execution of method3() to depend on the name, pass the name as a parameter. In general, you shouldn't use globals inside functions. It can become complicated to debug and maintain. An exception is members of a class, in which case your members are visible inside the methods and there is no need to pass them as parameters.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号