传递参数
大家好,我有一个问题,
如果我有一系列方法,例如:
Main()
{
Method1();
}
Method1()
{
Method2();
}
Method2()
{
Method3();
}
Method3()
{
ObtainsUserPermission(httpContext.Current.User.Name);
}
最好的方法是什么,在最后一个 Method3 中使用参数“httpContext.Current.User.Name”,或者在每个方法中传递参数?就像这样:
Main()
{
Method1(httpContext.Current.User.Name);
}
Method1(string name)
{
Method2(name);
}
Method2(string name)
{
Method3(name);
}
Method3(string name)
{
ObtainsUserPermission(name);
}
谢谢大家。
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" in 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这听起来像是神奇的参数。
一个好的经验法则是 - 如果您希望 method3() 的执行取决于名称,请将名称作为参数传递。一般来说,您不应该在函数内部使用全局变量。调试和维护可能会变得复杂。类的成员是一个例外,在这种情况下,您的成员在方法中可见,并且无需将它们作为参数传递。
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.